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

查看:212
本文介绍了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 = '/';

此操作已从网址中删除了网站/索引,但再次更改了网址,使其看起来像

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';

但是URL保持不变.我的问题是如何使它看起来像

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' => 'yii\web\UrlManager',
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'baseUrl' => 'http://example.dev',
    'rules' => [
        [
            'pattern' => '<page:\d+>',
            'route' => 'site/index'
        ]
    ],
]

和SiteController:

and SiteController:

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

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

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