While declaring the routes you must have added route id
and frontName
. In most cases, we use the same name for both. Why not use a different name? What is the difference between an id
and the frontName
inside routes.xml.
Table of Contents
routes.xml
The routes.xml file is an important file that routes HTTP requests to the appropriate controller inside Magento. When we write either a frontend or admin controllers that time we need to declare the routes.xml file under the etc/frontend
, etc/adminhtml
directory of the module which has controllers. The only difference between an admin request and a frontend request is the router id. In the case of admin it is “admin
” and for frontend it is “standard
“
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="learning_magento" frontName="learning_magento">
<module name="LearningMagento_Routings" />
</route>
</router>
</config>
You can also look how we can override a controller in Magento. Overriding a controller doesn’t require routes.xml file.
config element
The <config>
element is the root element in the xml file. The attribute xmlns:xsi must always have a value as http://www.w3.org/2001/XMLSchema-instance
which defines as namespace and xsi:noNamespaceSchemaLocation will have "urn:magento:framework:App/etc/routes.xsd
which is the schema location.
router element
As mentioned above this element has an id as an attribute it will always have two values admin and standard. If you place a router id as admin in etc/frontend
and standard in etc/adminhtml
then it won’t work. The standard one is for the front end and admin is for the back end. The above example is of the Admin route.
route element
This is the main part we are here to see. The difference between id
and frontName
. It has 3 attributes id
, frontName
and disabled
.
id attribute
The id
attribute should be unique from other routes. It is used by Magento to identify and match incoming requests.
frontName
The frontName
attribute specifies the URL segment that appears before the action name in the URL structure.
disabled
Both of the above two attributes are mandatory for the element apart from these two there is an optional attribute disabled, which is used to disable the route.
Difference between id and frontName
The key difference between id
and frontName
is that it is used for internal module identification in the routing system where as the frontName
is used determine the URL path prefix for accessing the controller.
<route id="learningmagento" frontName="magentolearn">
Let’s discuss with an example using the above route. A controller class is defined under Controller/Learn/Routing.php. To access this route it will the frontName
and not the id
. If you enter the URL of the website followed by the route id and the controller path then you will be redirected to a 404 page (learningmagento/learn/routing). This is because the id is not used for accessing the path.
The correct way would be to use frontName
as the route and the path to the controller. magentolearn/learn/routing this way the controller will be hit.
module element
This element has only one attribute which is the name
and it should be the module name. In our case it was LearningMagento_Routings
.