Zitty Yam
Zitty Yam

Reputation: 135

Yii, custom UrlManager::createUrl() crash the widget-create-link

I have create a custom UrlManager for adding language parameter at the beginning of the $route.

what it do is make $route="site/index" ---> $route="en/site/index" (or other current using language)

It is working, but I there is some page I don't want the "createUrl" rule apply, some widget which create Links (e.g. Pagination of CGridView, also the update button), will making error.

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)

Also, the link in Gii is also have some error.

How can I make my custom Url routing works with Yii's originally ecology ?


protected/config/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>',
        )
    )

protected/components/UrlManager.php

class UrlManager extends CUrlManager{

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

}

protected/components/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;
    }

  ...... 

}

Upvotes: 1

Views: 742

Answers (1)

Hanafi
Hanafi

Reputation: 151

Try to make language as a $_GET parameter.

In your custom "protected/components/UrlManager.php"

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

In your Language class, add this

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;
}

Upvotes: 0

Related Questions