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.
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.
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.
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!
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).