Slip
Slip

Reputation: 949

Trouble with UrlManager in Yii2

I have a directions module in my Yii2 app in which in DefaultController i have two actions index and view.

I need to create link with slug site.ru/directions/allergologyto use it with

 public function actionView($url) {
 }

I created URL:

<li><a href="<?=Url::to(['/directions/view','url' => $items->url]); ?>"><?=$items->name?></a></li>

And I added rules in UrlManager:

 '<module:directions>/<url:\w+>' => '<module>/view'

but i getting Page Not Found (#404) - yii\base\InvalidRouteException: Unable to resolve the request "directions/view".

I tried to use some rules like

'<module:directions>/<url:\w+>' => '<module>/<controller>/view',
'<module:directions>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<module:directions>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',

But it doesn't work too.

Upvotes: 0

Views: 40

Answers (1)

rob006
rob006

Reputation: 22174

You cannot skip controller name in this case - you need to specify it as a part of route in your rules:

 'directions/<url:\w+>' => 'directions/default/view',

And use full route on creating URLs:

<li><a href="<?=Url::to(['/directions/default/view','url' => $items->url]); ?>"><?=$items->name?></a></li>

Upvotes: 1

Related Questions