Reputation: 581
I'm using Yii2 advanced and Nginx. My urlmanager:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'test1' => 'page/view?id=1',
'test2' => 'page/view?id=2',
],
],
mydomain.com/page/view?id=1 - it works
mydomain.com/test1 - doesn't work. 404 error
It worked in Yii1 but doesn't work in Yii2. What's wrong? Thanks.
Upvotes: 0
Views: 138
Reputation: 1020
You can set array of default parameters in defaults property.
'rules' => [
[
'pattern' => 'test1',
'route' => 'page/view',
'defaults' => ['id' => 1],
],
],
Upvotes: 1
Reputation: 463
In your urlManager
config, you should define only controller and action.
So your rules
will be:
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'test1' => 'page/view',
'test2' => 'page/view',
],
],
You can run via link @baseUrl/test1?id=1
and @baseUrl/test2?id=2
, or use this urlHelper
in your view:
Html::a('test1', \yii\helpers\Url::to(['test1', 'id' => 1]))
If you want to test1
show page view with id=1
, test2
will show with id=2
, you should config rule like that:
'rules' => [
//other rules ...
[
'pattern' => 'test<id:\d+>',
'route' => 'page/view',
],
//other rules ...
],
Hope this useful.
Goodluck and have fun.
Upvotes: 0
Reputation: 910
Use regular expressions for parameters in a rules:
'rules' => [
'test/<id:\d+>' => 'page/view',
],
Also read Yii2 docs about Routing and URL Creation http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#routing
Upvotes: 0