How Routes Work in Magento

In Magento 2, routes are used to determine which module and controller will be responsible for handling a request.

When a request is made to the Magento server, the server uses the frontName value in the route to determine which module and controller should handle the request. The frontName value is specified in the routes.xml file of the module, and it is usually the first segment of the URL after the base URL. For example, if the frontName value is catalog, then a request to http://www.example.com/catalog/products/view/id/1 would be handled by the module and controller specified in the route.

The action value in the route specifies the action method in the controller that should be executed to handle the request. In the example URL above, the view value in the URL specifies the action method to be executed.

The id value in the URL is passed as a parameter to the action method. In this case, the id value would be 1.

Routes are defined in the etc/frontend/routes.xml file of a module, or in the etc/adminhtml/routes.xml file for backend routes.

Here is an example of a route definition in the etc/frontend/routes.xml file of a module:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="customroute" frontName="custom">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

This route definition specifies that requests to http://www.example.com/custom/* will be handled by the Vendor_Module module.

The Vendor_Module module will have a controller class with the name Controller\Custom\*, where * is the action specified in the URL. For example, a request to http://www.example.com/custom/index will be handled by the indexAction method in the Controller\Custom\Index controller class.

Here is an example of a route definition in the etc/frontend/routes.xml file of a module:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="customroute" frontName="custom">
            <module name="Vendor_Module" />
        </route>
    </router>
</config>

This route definition specifies that requests to http://www.example.com/custom/* will be handled by the Vendor_Module module.

The Vendor_Module module will have a controller class with the name Controller\Custom\*, where * is the action specified in the URL. For example, a request to http://www.example.com/custom/index will be handled by the indexAction method in the Controller\Custom\Index controller class.

Hope this is going to help you in understanding the basic routes and controllers idea how they work.
Thanks for reading!

0
Subscribe to my newsletter

Read articles from Charanjeet Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Charanjeet Singh
Charanjeet Singh