Yii2 如何从 url 中删除站点/索引和页面参数 [英] Yii2 How to remove site/index and page parameter from url

查看:21
本文介绍了Yii2 如何从 url 中删除站点/索引和页面参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在默认页面上使用分页,即在 yii2 中的站点/索引上.所以链接器为分页生成的 URL 看起来像这样

I am using pagination on default page,i.e, on site/index in yii2. So the URL generated by linker for paginations looks like this

domain.com/site/index?page=1

我想删除站点/索引和页面参数,使其如下所示

I want to remove site/index and page parameter so that it looks like as follows

domain.com/1


我尝试在这样的配置文件中的 URL 管理器中编写规则


I tried writing rule in URL manager in config file like this

'site/index/<page:d+>' => 'site/index'

这使得网址如下

domain.com/site/index/1


因此为了删除站点/索引,我将分页路径设置为/",如下所示


So to remove site/index as well, I set route of pagination to '/' like this

$pagination->route = '/';

这从 URL 中删除了站点/索引,但这又将 URL 更改为类似

This removed site/index from URL but this again changed the URL to look like

domain.com/?page=1


我尝试像这样更改 URL 管理器中的规则


I tried changing rule in URL manager like this

'/<page:d+>' =>'site/index';

但网址保持不变.我的问题是如何让它看起来像

But the URL remained the same. My question is how to make it look like

domain.com/1

我正在使用 Yii2 高级模板并在 URL 管理器中启用了严格解析.

I am using Yii2 advanced template and have enabled strict parsing in URL manager.

推荐答案

我使用以下组件配置在我的本地机器上运行了这个:

I got this working on my local machine using the following component configuration:

'urlManager' => [
    'class' => 'yiiwebUrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'baseUrl' => 'http://example.dev',
    'rules' => [
        [
            'pattern' => '<page:d+>',
            'route' => 'site/index'
        ]
    ],
]

和站点控制器:

public function actionIndex($page=NULL)
{
    var_dump($page);
    exit;
}

这篇关于Yii2 如何从 url 中删除站点/索引和页面参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆