luk
luk

Reputation: 355

Yii2 url rules using slug for multiple controllers eg domain.com/slug

I'd like to set the url rules for someting like: www.domain.com/slug in url for multiple controllers. I have one model "Content" and there is a column called content type in the "Content" model. Eg. content_type = 1 means contest, content_type = 2 means post, content_type = 3 means product etc. I have created a controller for each of the content_type, contestController, postController, productController.

I wonder if there is a way to keep the urls in the following format for few different controllers:

'<slug:.*?>'=>'contest/view', // content type contest - contestController
'<slug:.*?>'=>'post/view', // content type post - postController
'<slug:.*?>'=>'product/view', // content type product - productController

it does work with just one rule above as per yii documentation.

Upvotes: 1

Views: 187

Answers (1)

Ripper
Ripper

Reputation: 1162

Not direct answer but, have you considered another approach?

  1. Send everything to 1 controller

    '<slug:.*?>'=>'post/view',

    • on frontend part user will not know which controller is actually used, but you can detect type after you find model by slug.
  2. Split router by controller

    'post/<slug:.*?>'=>'post/view',

    • you should know, when placing link to that page, which type is used
  3. Split router by type flag (not tested)

    '<slug:[\w][\-\w]*?>/post'=>'post/view',

    '<slug:[\w][\-\w]*?>/product'=>'product/view',

Other then that I do not see how it could work.

Upvotes: 3

Related Questions