Yii2如何从url中删除站点/索引和页面参数

时间:2016-07-08 17:52:35

标签: pagination yii2

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

.bind(this)

我想删除site / index和page参数,使其如下所示

domain.com/site/index?page=1


我尝试在配置文件中在URL管理器中编写规则,如下所示

domain.com/1

这使得网址如下

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

所以为了删除网站/索引,我将分页路线设置为&#39; /&#39;像这样

domain.com/site/index/1

从网址中删除了网站/索引,但这又将网址更改为

$pagination->route = '/';


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

domain.com/?page=1

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

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

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

1 个答案:

答案 0 :(得分:1)

我使用以下组件配置在本地计算机上运行:

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

和SiteController:

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