It is not possible with ‘<slug:[\w\-]+>’ => … because everything will go this way. If there are only selected actions you want to make it this way you can do
The property ‘rules’ is an associative array (an array of key-value pairs) in which the key is an url pattern and the value is the corresponding route.
In the above, you are trying to add 3 elements to the array, but only the last one is actually added, because all the 3 elements share the same key ‘<slug:[\w\-]+>’. It’s just like you are writing:
$array = [
'a' => 'ABC',
'a' => 'DEF',
'a' => 'XYZ',
];
print_r($array);
// prints only 'a' => 'XYZ'
You have to define 3 different url patterns for pages, blog and product routes. Is there something in the slug which indicates the route? For example, slugs of ‘pages/main’ always start with ‘m-’ and those of ‘blog/posts’ with ‘p-’ and ‘product/list’ with ‘l-’, then you can create the url patterns accordingly:
If you can not recognize any distinguishing pattern in the slug, then probably you have to create a dedicated dispatching controller/action which analyse the slug and dispatches the request to the appropriate controller/action.
Thanks a lot for reply, to say truth I already tried with Custom URLRule but failed as was not sure how to make it correctly. I had created components folder inside my frontend folder, there I have created CustomRule.php file, extended that from URLRule but not sure what to do the next?
Sorry for my questions, this is my first project on Yii2 so I am a little confused.