Setting up a Magento module that includes both frontend and admin functionality is a common requirement for many custom projects. In this post, I’ll explain how to properly configure your routers so that they call the correct controllers based on whether the request comes from the front-end or the admin panel.
Understanding Magento Routing
Firstly, let’s clarify some key concepts about Magento routing. Each router is defined in the etc/frontend.xml and etc/adminhtml.xml configuration files inside your module. These routers map URLs to controller actions based on the specified route name and module configuration.
In your example, both frontend and admin routers point to the same module (Mypackage_Myodule) and frontName (Mymodule). This means that by default, they would indeed try to call the IndexController.php in the controllers directory of your module. However, we can modify this behavior to call different controllers for each context.
Defining Different Controllers
To have separate functionality for frontend and admin, you need to define two versions of the same controller - one in a controllers/IndexController.php file inside your main module folder, and another in a controllers/AdminController.php file inside an Adminhtml subfolder within your controllers directory. This is required because Magento doesn’t support routing to different controllers based on the context (frontend vs admin) out of the box.
Here’s how you can structure your module:
Myodule/
etc/
frontend.xml
adminhtml.xml
controllers/
Adminhtml/ # Folder for admin-specific controllers
AdminController.php
IndexController.php
Configuring the Admin Router
The mymoduleadmin router in your example is correctly configured to point to your module. However, it doesn’t specify a route name. To make it unique and map it to your AdminController, update your etc/adminhtml.xml file like this:
<config>
<adminhtml>
<routers>
<mymoduleadmin>
<use>admin</use>
<args>
<module>Mypackage_Myodule</module>
<frontName>Adminhtml</frontName> # Change this to match the subfolder
</args>
</mymoduleadmin>
</routers>
</adminhtml>
</config>
Notice that I’ve changed the frontName argument to match the Adminhtml subfolder where your admin controller is located.
The Frontend Router
Your frontend router is already configured correctly. It will map URLs starting with Mymodule/ to the standard IndexController.php inside your main controllers folder.
Conclusion
As you can see, it’s possible to have a single module with both front-end and admin functionality by properly configuring your routers and controllers. The key is to create separate controller versions for each context - one in the root controllers directory for frontend requests and another in an Adminhtml subfolder for admin requests.
Remember that Magento uses a flat structure for its routing configuration, so you need to manually manage which controllers are called based on the URL route name.
By following this approach, you can create efficient custom modules with cleanly separated functionality for both customer-facing and backend administrative tasks.