Adding url rules on the fly

I have some modules which come with their own url rules so I push them using a class which is loaded. The class tries to set it’s own rules for the module being loaded but it doesn’t work:


Yii::app()->urlManager->rules = array_merge ( Yii::app()->urlManager->rules, array ( 	

	'aspinner/install' => 'aspinner/as_main/install' 

) );

If I put the same rule in the main config file the it works. Any idea why?

Here’s a print after setting the rule on the fly from within the class:


CUrlManager Object

(

    [rules] => Array

        (

            [aspinner/install] => aspinner/as_main/install

        )


    [urlSuffix] => .html

    [showScriptName] => 1

    [appendParams] => 1

    [routeVar] => r

    [caseSensitive] => 1

    [cacheID] => cache

    [useStrictParsing] => 

    [_urlFormat:CUrlManager:private] => path

    [_rules:CUrlManager:private] => Array

        (

        )


    [_baseUrl:CUrlManager:private] => 

    [behaviors] => Array

        (

        )


    [_initialized:CApplicationComponent:private] => 1

    [_e:CComponent:private] => 

    [_m:CComponent:private] => 

)

As you can see, the rule is added but not counted as my application fails to use it.

You have to re-init the UrlManager after you add additional rules (because the actual processed rules are written to CUrlManager::$_rules).


Yii::app()->urlmanager->init();

Also before that you can just do:


Yii::app()->urlmanager->rules[] = array('aspinner/install' => 'aspinner/as_main/install');


Yii::app()->urlmanager->init();

Thanks! That worked.