How to exclude full index path from the routes


(Koredalin) #1

Hello All,

I have made a post in Facebook: "Yii2 PHP framework" group "/yii2talk/permalink/2037978059776059/".

I understood that it’s place is here, by the moderator announcement. So, I ask you here too. If needed - I’ll remove the post in Facebook.

I have read many tutorials, but I cannot find one routing configuration.

I have a question about the routes in Yii2.

I’m interested in how to disable the “<controller>/index” pattern from the below passage, but keep only “<controller>” pointing to “<controller>/index”.

Please, advise me.

File: "/config/web.php".

‘urlManager’ => [

'enablePrettyUrl' =&gt; true,


'enableStrictParsing' =&gt; true,


'rules' =&gt; [


	'&lt;controller&gt;' =&gt; '&lt;controller&gt;/index',


	'&lt;controller&gt;/&lt;action&gt;' =&gt; '&lt;controller&gt;/&lt;action&gt;',


],

],

I don’t like the idea to make exceptions in the controllers, because I have to make some in any controller.

Thank you, all!


(Softark) #2

Hi koredalin, welcome to the forum!

The rules should be something like the following:




    'rules' => [

        '<controller:\w+>' => '<controller>/index',

        '<controller:\w+>/<action:\w+>' => '<controller>/<action>',

     ],



Note that the keys in the array of rules are the url patterns, and the values the routes.

https://www.yiiframework.com/doc/guide/2.0/en/runtime-routing#url-rules


(Koredalin) #3

Hi softark,

This is not, what I have looked for. I found my answer, but now I have issues with the main controller. I’ll type here the result, when I’m done.

Meanwhile, can you please have a look at my next Yii forum question: https://www.yiiframework.com/forum/index.php/topic/76844-yiiwebcontroller-inheritance/page__p__316741__fromsearch__1#entry316741

Thank you