Yii,自定义UrlManager :: createUrl()使widget-create-link崩溃

时间:2014-08-21 02:11:36

标签: php yii yii-components yii-routing

我已经创建了一个自定义UrlManager,用于在$ route的开头添加语言参数。

它的作用是使$ route =" site / index" ---> $路线="烯/站点/索引" (或其他当前使用的语言)

它正在运作,但我有一些页面我不想要" createUrl"规则适用,创建链接的一些小部件(例如CGridView的分页,也是更新按钮)将产生错误。

URL of the page        : en/controller/admin/
pagination URL created : en/controller/admin/language/en/model_page/2 (error)
what I want            : en/controller/admin?model_page=2             (this will work)

此外,Gii中的链接也有一些错误。

如何让我的自定义网址路由与Yii的原生态一起使用?


保护/配置/ main.php

    'urlManager'=>array(

        'class' =>'application.components.UrlManager',
        'urlFormat'=>'path',
        'showScriptName'=>false,
        'caseSensitive'=>false,     
        'rules'=>array(
            '<language:(fr|en)>/<controller:\w+>/<action:\w+>/id/<id:\d+>'=>'<controller>/<action>',
            '<language:(fr|en)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<language:(fr|en)>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>', 
            '<language:(fr|en)>/<controller:\w+>'=>'<controller>',
        )
    )

保护/组件/ UrlManager.php

class UrlManager extends CUrlManager{

    public function createUrl($route,$params=array(),$ampersand='&'){
        $route = Language::addRouteLang($route);
        return parent::createUrl($route, $params, $ampersand);
    }

}

保护/组件/ Language.php

class Language extends CComponent{


    public static $current_lang;
    public static $current_lang_id;


    public static function getCurrentLang(){
        return self::$current_lang_id;
    }


    public static function addRouteLang($route){
        //To do : make this get data from dataBase
        $langList = array( 'fr', 'en');

        //get first para of $route
        $routeArr = explode("/",$route );
        $firstPara = $routeArr[0];

        if (! in_array($firstPara, $langList)){
            //there is no language parameter

            if (Yii::app()->user->hasState('language')){
                //check user State
                $lang = Yii::app()->user->getState('language');
                $route = $lang.'/'.$route;
            }else if(isset(Yii::app()->request->cookies['language'])){
                //check cookies
                $lang = Yii::app()->request->cookies['language']->value;
                $route = $lang.'/'.$route;
            }
        }
        return $route;
    }

  ...... 

}

1 个答案:

答案 0 :(得分:0)

尝试将语言设为$ _GET参数。

在您的自定义&#34; protected / components / UrlManager.php&#34;

public function createUrl($route,$params=array(),$ampersand='&'){
    $params = Language::addParamLang($params);
    return parent::createUrl($route, $params, $ampersand);
}

在您的语言课程中,添加此

public static function addParamLang($params){
    //To do : make this get data from dataBase
    $langList = array( 'fr', 'en');

    if (Yii::app()->user->hasState('language')){
        //check user State
        $activeLanguage = Yii::app()->user->getState('language');
    }else if(isset(Yii::app()->request->cookies['language'])){
        //check cookies
        $activeLanguage = Yii::app()->request->cookies['language']->value;
    }else{
        //default language 
        $activeLanguage = 'fr';
        /* additional codes here... */
    }

    if(! isset($params['language'])){
        $params['language']=$activeLanguage;
    } else if (! in_array($params['language'], $langList)){
        $params['language']=$activeLanguage;
    }

    return $params;
}