我在多语言网址路由方面遇到问题
如何转换
http://localhost/testimonialDetails/pinturas-mataavenida/language/en
到
http://localhost/en/testimonialDetails/pinturas-mataavenida
问题是当URL中的任何“ - ”slug时,createUrl()函数无法路由此。是我的代码上的这个问题
我的功能
$这 - > createUrl( “testimonialDetails / $ testimonials-> URL”);
http://localhost/testimonialDetails/pinturas-mataavenida/language/en
如果我用“_”(下划线)替换“ - ”
http://localhost/en/testimonialDetails/pinturas_mataavenida
然后它工作正常
覆盖createUrl函数
class UrlManager extends CUrlManager {
public function createUrl ($route, $params = array(), $ampersand = '&') {
if (!isset($params['language'])) {
if (Yii::app()->user->hasState('language'))
Yii::app()->language = Yii::app()->user->getState('language');
else if (isset(Yii::app()->request->cookies['language']))
Yii::app()->language = Yii::app()->request->cookies['language']->value;
$params['language'] = Yii::app()->language;
}
return parent::createUrl($route, $params, $ampersand);
}
}
这是我的urlManager规则
'<language:(es|da|en)>/<module:\w+>/product/application/<id:\d+>' => '<module>/product/application',
'<language:(es|da|en)>/' => 'site/index',
'<language:(es|da|en)>/testimonialDetails/<url:[\w-]+>' => 'testimonials/details',
'<language:(es|da|en)>/productdetails/<url:.+>' => 'product/productdetails',
//'productdetails/<url:.+>' => 'product/productdetails',
'<language:(es|da|en)>/<action:(contact)>/*' => 'site/<action>',
'<language:(es|da|en)>/<controller:\w+>' => '<controller>',
'<language:(es|da|en)>/<controller:\w+>/<id:\d+>' => '<controller>/view',
'<language:(es|da|en)>/<module:\w+>/<controller:(admin|profileField)>/<id:\d+>' => '<module>/<controller>/view',
'<language:(es|da|en)>/<module:\w+>/<controller:(admin|profileField)>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
'<language:(es|da|en)>/<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
'<language:(es|da|en)>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<language:(es|da|en)>/<controller:\w+>/<action:\w+>/*' => '<controller>/<action>',
我有没有想念。请帮忙,
抱歉我的英文不好提前。