Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Module custom router with annotations

Hello.

I am trying to make a modular app, where I would like to put a router inside every module.

How do I make a prefix for every module, so that if you enter url "/admin" the module admin router will be loaded, and if you enter url "/frontpage" the module frontpage router will be loaded?

I was searching around, and the only thing I can find, is to put everything into ONE router file, but i would like to avoid that

Also the code: $router->addModuleResource('admin','Index'); does not work. I have to make a direct namespace to where the module exists.

Does anyone know how to put router inside modules? Thank you!



116.1k
Accepted
answer
edited Apr '20

I haven't used modules very much, so maybe my answer is naive: I don't think you can have module-specific routers because If you don't have a central router, how does a request get routed to the module?

Say you've got a URL: https://myapp.com/admin/store/edit/23, where admin is the module name, store the controller name, edit the action name, and 23 is the parameter for the action.

If your router is in the admin module, how would a request to the above URL get routed? Despite using multiple modules, all requests come in through a central point, that then routes the traffic. Without that central router, there's no way the request can get routed to the proper module.

What is the problem you're trying to solve by not using a single central router? If it's because you don't want 1 big monolithic file with all the routes, you can break the routes out into separate config files, then load them all into the router. Practically speaking though, you shouldn't need a whole lot of custom routes. At least, I've never encountered a situation where I had so many routes I had to be concerned with organizing them.



1.8k

Thank you for your answer.

I ended up with a single router, using annotations, the router file wouldent end up being that big anyway.. I just wanted to structure my routes best way possible :-)

I suppose if you really wanted to, you could break the routes for each module into a separate config file that is stored with the rest of the files for the module. Then in your central router, load those separate config files.

Even with modules, some things are central though.