Reputation: 3584
I'm trying to modify two similar custom modules to combine them into 1 custom module. However, both of these modules use the <routers>
tag in the confix.xml. I've searched and searched for a way to do this but no luck.
For example:
Custom Module 1
<admin>
<routers>
<Namespace_Module1>
<use>admin</use>
<args>
<module>Namespace_Module1</module>
<frontName>frontname</frontName>
</args>
</Namespace_Module1>
</routers>
</admin>
Custom Module 2
<admin>
<routers>
<Namespace_Module1>
<use>admin</use>
<args>
<module>Namespace_Module2</module>
<frontName>namefront</frontName>
</args>
</Namespace_Module2>
</routers>
</admin>
I think this is the only thing stopping me from being able to combine the two similar modules together. Can this be done? If so, how (an example)?
Upvotes: 1
Views: 4652
Reputation: 12706
As I understand, you are trying to match controllers for 2 modules through one frontName.
This is perfectly possible by using the 'modules' node in your second module. Keep your configuration for the first module, but use this for the second one:
<admin>
<routers>
<Namespace_Module1>
<args>
<modules>
<Namespace_Module2>Namespace_Module2</Namespace_Module2>
</modules>
</args>
</Namespace_Module1>
</routers>
</admin>
No need to specify 'use' or 'frontName', those XML files are merged anyway. Magento will first try to find a matching controller in the first module, and then in the second one.
Upvotes: 0
Reputation: 10114
Im not entirely sure what you are trying to achieve. Are you looking for two different routers i.e. two distinct front names, defined from within a single module?. If so, read on - if not, can you clarify things a little further please.
Firstly, you have a tag mismatch in the xml you have provided in Custom Module 2. You are opening <Namespace_Module1>
and closing with </Namespace_Module2>
- so as it stands, this code will not work.
Secondly, to define a router, you must use the <routers>
tag - so there is no issue with this. The nodes directly underneath it must be unique though.
So, assuming I have read your question correctly and you want to merge these two router nodes but still have two distinct front names, the following would work:
<admin>
<routers>
<namespace_module1>
<use>admin</use>
<args>
<module>Namespace_Module1</module>
<frontName>frontname</frontName>
</args>
</namespace_module1>
<namespace_module2>
<use>admin</use>
<args>
<module>Namespace_Module2</module>
<frontName>namefront</frontName>
</args>
</namespace_module2>
</routers>
</admin>
Though, if there was a particular reason that these have to be separate routers, then I would offer the following as better alternative: use a single router but multiple controllers. So your xml would just be:
<admin>
<routers>
<namespace_module>
<use>admin</use>
<args>
<module>Namespace_Module</module>
<frontName>frontname</frontName>
</args>
</namespace_module>
</routers>
</admin>
Create two controllers in your modules controller directory, say Module1Controller.php
and Module2Controller.php
.
Then you would be able to access them (the index actions) via /frontname/module1/
and /frontname/module2
.
This feels like a much cleaner solution.
Upvotes: 4