E
E
EVOSandru62014-12-11 09:42:50
Yii
EVOSandru6, 2014-12-11 09:42:50

How can I override CUrlManager rules in a Yii module?

such a problem, inherited from CUrlManager:

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

And now I have ru/ or another language/ prefix in the url immediately after the domain. No
glitches have been observed in the client yet, but in the admin panel module I can get to the actions of the desired controller only if I remove ru/ or another language/ in the address bar . For some reason, in the links you need to indicate the names of the module after the language prefix through /, so as not to fly out to the client part.
Here is the Url Manager Configuration:
'urlManager'=>array(
            'class'=>'application.components.UrlManager',
            'urlFormat'=>'path',
            'showScriptName'=>false,
            'rules'=>array(
                '<language:(ru|en|kz|de|ch|tu|ar)>/' => 'site/index',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<action:(contact|login|logout)>/*' => 'site/<action>',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<controller:\w+>/<action:\w+>/*'=>'<controller>/<action>',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<module:\w+>/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>/<id>',
                '<language:(ru|en|kz|de|ch|tu|ar)>/<module:\w+>/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',

            ),
        ),

As I think, the solution here can be as follows:
1. Create a new config in the admin panel and not use and comment out 'class'=>'application.components.UrlManager' in it (but then I won't be able to use multilanguage in the admin panel and don't know how to connect config inside the module)
2. How to finish the rules of the main config
Help, good people! Considerations and references on both points are very interesting.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Michael, 2015-07-12
@EVOSandru6

Look at the implementation https://github.com/mishamx/yii-multilanguage although not everything is perfect there either, short addresses cannot be used in modules without a module name

A
Arks, 2014-12-11
@Arks

no one bothers to create a separate config and entry point for the admin panel or separate it at the application level.
Both configs must be inherited from the base one. It is clear that it is somehow strange to interfere with everything in a heap.

I
Ivan, 2015-01-30
@IvanCher

The rules are compared from top to bottom and the first match is taken.
For example, if the request /ru/edit-post/12 should send us to the admin module along the path admin-module/post/edit and pass the post id 12. Then the rules look something like this:

'rules'=>array(
    '<language:(ru|en|kz|de|ch|tu|ar)>/' => 'site/index',
    '<language:(ru|en|kz|de|ch|tu|ar)>/<action:edit-post>/<id:\d+>' => 'admin-module/post/<action>',
    ...
),

But, of course, it's not very nice to get to list all the module actions you need. Therefore, write your own rule class and put all the logic in it. I don’t know what modules and controllers you have, and what paths are required for them, so I can’t tell you here.
You can read about custom url rule classes here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question