set urlManager in a module

I cannot seem to set urlManager component in a module. I have module for which I would like to specify a custom urlmanager class that should be active for the specific module only.

I have the following code in the moduleclass init():








the above code is not working. When I declare the urlManager in config/main.php it works well but I don’t want to declare a custom urlManager class across the whole application but only across one module.

any thoughts would be welcome.



You need to set the UrlManager before it is used. In a module is definitely too late, as the manager itself is responsible for resolving a request to a module/controller/action.


which is the best way to do that ? (Yii 1.1.x)

Well, you can not set the URL rule in a module. As explained above that’s logically not possible since the URL manager is used to determine the currently active module. So by the time the module is activated all URL resolving is already done and adding URL rules then is meaningless.

There is a similar discussion about that

I solve this issue but I don’t like this approach

  1. detect by code if module will be executed in main index.php of web application

  2. load an extra config/yourModule.php that use different UrlManager settings

The problem is "detect the module by code" due variety of urlmanager rules settings…

What is your opinion?

I don’t know what you try to achieve. But I can only repeat myself: This does not make sense! Any “solution” can only be a dirty hack. It’s like asking “How can I change my parents?”. You can’t!

Ok lets explain.

In my case I have a module eshop that I put slug categories and products name in url.

The slug generated dynamically by database model category and product model, so rules in urlmanager cannot help, so I sould have a seperated UrlManager class for eshop module. The UrlManager for the main website should be different because works in different way and I have many createUrl in eshop to change it like $this->createUrlEshopWithslug(…)

I’d say all you need is to write a custom UrlRule class, which can create/parse you slugged URLs. Then if you install this module somewhere, you need to configure this UrlRule manually in your urlManager component. That would be part of the installation instructions of your module (if you want to release your module or something).