A Complete Tutorial On Magento 2 Routing
Merry Christmas 🎅
Magento 2 routing defines a URL of the module.
The entire Magento 2 application flow is based on processing URL request and router classes which are responsible for matching and processing that requests.
The request URL in Magento 2 is http://example.com/index.php/router_name/controller/action
Here, the router_name is used to find the module.
When a request is made in Magento 2, controller/action: index.php → HTTP app → FrontController → Routing → Controller processing → etc flow is followed.
The FrontController is called in Http class to routing the request which will find the controller/action match.
vendor/magento/framework/App/FrontController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
use Magento\Framework\App\Action\AbstractAction; use Magento\Framework\App\RouterInterface; use Magento\Framework\Exception\NotFoundException; use Magento\Framework\Profiler; { Profiler::start('routers_match'); $routingCycleCounter = 0; $result = null; while (!$request->isDispatched() && $routingCycleCounter++ < 100) { /** @var RouterInterface $router */ foreach ($this->_routerList as $router) { try { $actionInstance = $router->match($request); if ($actionInstance) { $request->setDispatched(true); $this->response->setNoCacheHeaders(); if ($actionInstance instanceof AbstractAction) { $result = $actionInstance->dispatch($request); } else { $result = $actionInstance->execute(); } break; } } catch (NotFoundException $e) { $request->initForward(); $request->setActionName('noroute'); $request->setDispatched(false); break; } } } Profiler::stop('routers_match'); if ($routingCycleCounter > 100) { throw new LogicException('Front controller reached 100 router match iterations'); } return $result; } |
Steps to create a custom route on frontend and admin:
Prior to implementing the below method, you may refer to the Magento 2 module development tutorial.
Create a frontend route:
Create routes.xml in app/code/[Vendor]/[Module]/etc/frontend Folder add the following code:
1 2 3 4 5 6 7 8 9 |
<?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="standard"> <route frontName="helloworld" id="helloworld"> <module name="[Vendor]_[Module]"/> </route> </router> </config> |
Create admin route:
Create routes.xml in app/code/[Vendor]/[Module]/etc/adminhtml folder and add the following code:
1 2 3 4 5 6 7 8 9 |
<?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="helloworld" frontName="helloworld"> <module name="[Vendor]_[Module]"/> </route> </router> </config> |
Use route to rewrite controller:
Create routes.xml in app/code/[Vendor]/[Module]/etc/frontend Folder and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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="standard"> <route frontName="helloworld" id="helloworld"> <module name="[Vendor]_[Module]"/> </route> <route id="account"> <module name="[Vendor]_[Module]" before="Magento_Customer"/> </route> </router> </config> |
That’s all about Magento 2 Routing!
What are your opinions and experiences with routers in Magento 2? Don’t forget to share them in the comment section below.
In case you feel you need some extra help, feel free to get in touch through the comment section and I will be there to help!
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Solved – Undefined index: jobs Error On Running Cron In Magento 2.X.X
DIY: Create Payment Method in Magento 2 Using Our Guide
Next