How to config yii2 urlManager rules with language GET parameter and hide Controller name

For my advanced yii2 project, i added this rule to the common config to support multilanguage view:


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

This is working fine, and localhost/en/site/about point to localhost/site/about.

Now i want to hide site controller in the url. I try this:


'<lang:\w+>/<alias:\w+>' => '<lang>/site/<alias>',

Yii converts the url from about to localhost/en/about, but it does not work. t shows me an Error 404 not found.

Any idea?

Thanks!!

Try:





// ... in config/main.php


// the key of rule should be regexp, but value - should be a pair of 'controller/action', not URL (!)


'<lang:\w+>/<alias:\w+>' => 'site/alias'  


// ... in SiteController


class SiteController extend Controller

{

    public function actionAlias($lang, $alias)

    {

        ...

    }

}




And why are you trying to get ‘localhost:8888/multi/en/about’.

You regular expression ‘<lang:\w+>/<alias:\w+>’ match ‘localhost:8888/xxxxx/yyyyy’

If you want process urls like ‘localhost:8888/multi/en/about’ you should create rule:




'<some_variable:\w+>/<lang:\w+>/<alias:\w+>' => 'site/alias'



Thanks for your answer, mikezimin.

Multi is the folder where i have installed advanced yii2 project, not a variable. Sorry for confusion.

For a project, I have to hide the controller name and insert language code at the start of the route.

For example, en/about point to site/about with the $lang parameters passed in GET.

Is it possible to do both with urlManager rules?

Thanks again.

Hello mikezimin,

I added this to config/main.php:


'<lang:\w+>/<alias:\w+>' => 'site/alias' 

It does not work. Keep giving an Error 404.

Did you try this?


'<lang:\w+>/<alias:\w+>' => 'site/<alias>' 

It works!!!!


'<lang:\w+>/<alias:\w+>' => 'site/<alias>'

Thank you very much tri.