TL;DR…read the title.
If you have the patient, read the following.
I have a multilingual website that allow admin to add and remove language whenever he wanted.
I have the following routes:
‘<language>/medias’=>‘site/medias’,
‘<language>/about/<slug>’=>‘site/about’,
I have extended CUrlManager:
class UrlManager extends CUrlManager
{
public function createUrl($route,$params=array(),$ampersand='&')
{
if ( !isset($params['language']) ) {
if ( Yii::app()->user->hasState('language') )
$language = Yii::app()->user->getState('language');
else
$language = Language::main(); // get default language from the database
} else {
if ( Language::useful($params['language']) )
$language = $params['language'];
else
$language = Language::main(); // get default language from the database
}
$params['language'] = $language;
return parent::createUrl($route, $params, $ampersand);
}
}
…so that when I type this:
$this->createUrl('site/about', array('slug'=>'our-team'));
it will automatically append a language code and generate this:
/en/about/our-team
The problem is when I tried to implement a language switcher.
For example, when you are at this url: /en/about/our-team, if you click the language switcher, I want to redirect user to /fr/about/our-team
For now, I use this method:
$baseUrl . '/' . $avaliable_language['code'] . strstr(Yii::app()->request->pathInfo, '/')
It works fine but I was thinking is there a more elegant way to do this using the createUrl method. So what I couldn’t figure out right now is how can I get the current controller, action and parameter (i.e slug) inside a view.