Antuan
(Antonio Traverso)
November 15, 2016, 2:13pm
1
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!!
mikezimin
(Mzimin)
November 15, 2016, 3:47pm
2
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'
Antuan
(Antonio Traverso)
November 15, 2016, 5:01pm
3
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.
Antuan
(Antonio Traverso)
November 16, 2016, 9:59am
4
mikezimin:
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'
Hello mikezimin,
I added this to config/main.php:
'<lang:\w+>/<alias:\w+>' => 'site/alias'
It does not work. Keep giving an Error 404.
tri
(tri - Tommy Riboe)
November 16, 2016, 11:04am
5
Antuan:
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>'
Antuan
(Antonio Traverso)
November 16, 2016, 2:19pm
6
It works!!!!
'<lang:\w+>/<alias:\w+>' => 'site/<alias>'
Thank you very much tri.