{"id":719,"date":"2019-12-25T10:00:22","date_gmt":"2019-12-25T10:00:22","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/25\/magento-2-routing\/"},"modified":"2025-05-21T17:57:33","modified_gmt":"2025-05-21T12:27:33","slug":"magento-2-routing","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/magento-2-routing\/","title":{"rendered":"A Complete Tutorial On Magento 2 Routing"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><em>Merry Christmas  <\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Magento 2 routing<\/strong> defines a URL of the module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The entire Magento 2 application flow is based on processing URL request and router classes which are responsible for matching and processing that requests.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The request URL in Magento 2 is http:\/\/example.com\/index.php\/router_name\/controller\/action<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the <strong>router_name<\/strong> is used to find the module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When a request is made in Magento 2,&nbsp;<strong>controller\/action: index.php \u2192 HTTP app \u2192 FrontController \u2192 Routing \u2192 Controller processing \u2192 etc<\/strong> flow is followed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The FrontController is called in Http class to routing the request which will find the controller\/action match.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><em>vendor\/magento\/framework\/App\/FrontController.php<\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">use Magento\\Framework\\App\\Action\\AbstractAction;\nuse Magento\\Framework\\App\\RouterInterface;\nuse Magento\\Framework\\Exception\\NotFoundException;\nuse Magento\\Framework\\Profiler;\n\n{\n    Profiler::start('routers_match');\n    $routingCycleCounter = 0;\n    $result = null;\n    while (!$request->isDispatched() &amp;&amp; $routingCycleCounter++ &lt; 100) {\n        \/** @var RouterInterface $router *\/\n        foreach ($this->_routerList as $router) {\n            try {\n                $actionInstance = $router->match($request);\n                if ($actionInstance) {\n                    $request->setDispatched(true);\n                    $this->response->setNoCacheHeaders();\n                    if ($actionInstance instanceof AbstractAction) {\n                        $result = $actionInstance->dispatch($request);\n                    } else {\n                        $result = $actionInstance->execute();\n                    }\n                    break;\n                }\n            } catch (NotFoundException $e) {\n                $request->initForward();\n                $request->setActionName('noroute');\n                $request->setDispatched(false);\n                break;\n            }\n        }\n    }\n    Profiler::stop('routers_match');\n    if ($routingCycleCounter > 100) {\n        throw new LogicException('Front controller reached 100 router match iterations');\n    }\n    return $result;\n}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to create a custom route on frontend and admin:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prior to implementing the below method, you may refer to the <a href=\"https:\/\/meetanshi.com\/blog\/magento-2-module-development\/\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 module development tutorial<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a frontend route:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create <strong>routes.xml<\/strong> in <strong><em>app\/code\/[Vendor]\/[Module]\/etc\/frontend<\/em><\/strong> Folder add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" ?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"standard\">\n        &lt;route frontName=\"helloworld\" id=\"helloworld\">\n            &lt;module name=\"[Vendor]_[Module]\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create admin route:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create <strong>routes.xml<\/strong> in <strong><em>app\/code\/[Vendor]\/[Module]\/etc\/adminhtml<\/em><\/strong> folder and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"admin\">\n        &lt;route id=\"helloworld\" frontName=\"helloworld\">\n            &lt;module name=\"[Vendor]_[Module]\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Use route to rewrite controller:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create <strong>routes.xml<\/strong> in <strong><em>app\/code\/[Vendor]\/[Module]\/etc\/frontend<\/em><\/strong> Folder and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"standard\">\n        &lt;route frontName=\"helloworld\" id=\"helloworld\">\n            &lt;module name=\"[Vendor]_[Module]\"\/>\n        &lt;\/route>\n        &lt;route id=\"account\">\n            &lt;module name=\"[Vendor]_[Module]\" before=\"Magento_Customer\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s all about Magento 2 Routing!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-719","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=719"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions"}],"predecessor-version":[{"id":14012,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/719\/revisions\/14012"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}