Yii2 How to remove site/index and page parameter f

2019-03-02 10:31发布

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


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

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

This made the url like as follows

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


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

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

1条回答
不美不萌又怎样
2楼-- · 2019-03-02 11:18

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'
        ]
    ],
]

and SiteController:

public function actionIndex($page=NULL)
{
    var_dump($page);
    exit;
}
查看更多
登录 后发表回答