{"id":15477,"date":"2018-04-12T14:59:07","date_gmt":"2018-04-12T11:59:07","guid":{"rendered":"https:\/\/belvg.com\/blog\/?p=15477"},"modified":"2021-02-18T13:48:41","modified_gmt":"2021-02-18T11:48:41","slug":"controllers-routers-and-responses-in-magento-2","status":"publish","type":"post","link":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html","title":{"rendered":"Controllers, Routers and Responses in Magento 2"},"content":{"rendered":"\n<p>We continue to explore the latest version of the most popular ecommerce platform and today we are going to learn about Request routing in Magento 2, namely Front controller, Controllers, Routers and Responses. Technical extracts are included in the article, as usual, to make it more practical. Two more Magento 2 How-to&rsquo;s are going to be published next week.<\/p>\n<p>So let&rsquo;s get down to work.<\/p>\n<p><!--more--><\/p>\n<h2>Front Controller<\/h2>\n<p>Routing in Magento 2 is based on Front Controller Pattern. Front Controller is a design pattern, in which one component is responsible for processing all the incoming inquiries, their redirection to the corresponding components, further processing of the results and returning the results back to the browser.<\/p>\n<p>In Magento 2, this path starts from creating the front controller instance and subsequent calling <b>\\Magento\\Framework\\App\\FrontControllerInterface::dispatch<\/b> method in <b>\\Magento\\Framework\\App\\Http::launch<\/b> method.<\/p>\n<pre class=\"lang:php decode:true\">$frontController = $this-&gt;_objectManager-&gt;get(\\Magento\\Framework\\App\\FrontControllerInterface::class);\r\n$result = $frontController-&gt;dispatch($this-&gt;_request);\r\npublic function dispatch(RequestInterface $request)\r\n{\r\n&nbsp;&nbsp;\\Magento\\Framework\\Profiler::start('routers_match');\r\n&nbsp;&nbsp;$routingCycleCounter = 0;\r\n&nbsp;&nbsp;$result = null;\r\n&nbsp;&nbsp;while (!$request-&gt;isDispatched() &amp;&amp; $routingCycleCounter++ &lt; 100) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/** @var \\Magento\\Framework\\App\\RouterInterface $router *\/\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach ($this-&gt;_routerList as $router) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$actionInstance = $router-&gt;match($request);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($actionInstance) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request-&gt;setDispatched(true);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&gt;response-&gt;setNoCacheHeaders();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($actionInstance instanceof \\Magento\\Framework\\App\\Action\\AbstractAction) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = $actionInstance-&gt;dispatch($request);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} else {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = $actionInstance-&gt;execute();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (\\Magento\\Framework\\Exception\\NotFoundException $e) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request-&gt;initForward();\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request-&gt;setActionName('noroute');\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request-&gt;setDispatched(false);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;\\Magento\\Framework\\Profiler::stop('routers_match');\r\n&nbsp;&nbsp;if ($routingCycleCounter &gt; 100) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw new \\LogicException('Front controller reached 100 router match iterations');\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;return $result;\r\n}<\/pre>\n<p><span style=\"font-weight: 400;\">If the required path is found successfully, FrontController iterates through the available routers call the method <\/span><b>\\Magento\\Framework\\App\\Action\\AbstractAction::dispatch<\/b><span style=\"font-weight: 400;\"> of the corresponding controller.<\/span><\/p>\n<h2>Routers<\/h2>\n<p><span style=\"font-weight: 400;\">All routers in Magento 2 should implement <\/span><b>\\Magento\\Framework\\App\\RouterInterface <\/b><span style=\"font-weight: 400;\">interface and define <\/span><b>\\Magento\\Framework\\App\\RouterInterface::match<\/b><span style=\"font-weight: 400;\"> method. This method is responsible for matching and processing URL inquiries. In case of a successful match the router returns the corresponding controller instance. If the necessary controller is not found, Front Controller moves on to the next router. <\/span><\/p>\n<p>Magento 2 comes with 4 routers:<\/p>\n<ol>\n<li style=\"font-weight: 400;\"><b>Base Router<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\App\\Router\\Base<\/span><\/i><span style=\"font-weight: 400;\">) sets module front name, controller and action names, controller module and route name if found. Processes standard Magento URLs.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>CMS Router <\/b><span style=\"font-weight: 400;\">(<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Cms\\Controller\\Router<\/span><\/i><span style=\"font-weight: 400;\">) is used for processing CMS pages. Sets module name to &ldquo;cms&rdquo;, controller name to &ldquo;page&rdquo;, action name to &ldquo;view&rdquo; and page id depending on the requested page. After that it forwards request but won&rsquo;t dispatch it. This will lead to Front Controller starting the next cycle of router checking, on which Base Router based on the established path will call <\/span><strong>\\Magento\\Cms\\Controller\\Page\\View<\/strong><span style=\"font-weight: 400;\"><strong>,<\/strong> which will show the necessary page.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>UrlRewrite Router<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\UrlRewrite\\Controller\\Router<\/span><\/i><span style=\"font-weight: 400;\">) is responsible for catalog URL rewrites. It uses Url Finder to find the necessary URL in the database, and after that returns the forward just like CMS Router.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Default Router<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\App\\Router\\DefaultRouter<\/span><\/i><span style=\"font-weight: 400;\">) is used when no other router is able to find the appropriate controller. It&rsquo;s responsible for 404 Page.&nbsp;&nbsp;<\/span><\/li>\n<\/ol>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-audit\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Audit<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento Audit<\/p><a href=\"https:\/\/belvg.com\/magento-audit\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Visit the page<\/span><\/a><\/div><\/div><\/div>\n<h2><span style=\"font-weight: 400;\">Controllers<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Controllers in Magento 2 differ from typical controllers in MVC applications. Magento 2 controllers are responsible for only one specific URL and contain only one execute method. This method is responsible for returning result object and occasional processing of input POST data. All controllers inherit <\/span><b>\\Magento\\Framework\\App\\Action\\Action<\/b><span style=\"font-weight: 400;\"> class.<\/span><\/p>\n<div class=\"content_sales_info\" style=\"background-image: url(https:\/\/belvg.com\/blog\/wp-content\/uploads\/2019\/03\/Sales_background.png\"><img width=\"65\" height=\"65\" src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2015\/11\/Andrey_Dubina.png\" alt=\"Andrey_Dubina\" title=\"Controllers, Routers and Responses in Magento 2\"><div class=\"content_part\"><h3 class=\"title\">Partner With Us <\/h3><span class=\"sales_content_text\">Let's discuss how to grow your business. Get a Free Quote.<\/span><\/div> <a class=\"call-to-action\" href=\"https:\/\/belvg.com\/contact?utm_source=blog&amp;utm_campaign=sales_block&amp;utm_content=middle\" target=\"_blank\" rel=\"noopener\">Talk to Andrey<\/a><\/div>\n<p><span style=\"font-weight: 400;\">The required controller is searched in the Base Router, and then it&rsquo;s called in the Front Controller.<\/span><\/p>\n<pre class=\"lang:php decode:true\">public function match(\\Magento\\Framework\\App\\RequestInterface $request)\r\n{\r\n&nbsp;&nbsp;$params = $this-&gt;parseRequest($request);\r\n&nbsp;&nbsp;return $this-&gt;matchAction($request, $params);\r\n}<\/pre>\n<p><b>\\Magento\\Framework\\App\\Router\\Base::parseRequest <\/b><span style=\"font-weight: 400;\">method is responsible for splitting the requested URL into segments, and \\<\/span><b>Magento\\Framework\\App\\Router\\Base::matchAction<\/b><span style=\"font-weight: 400;\"> method tries to find a suitable controller based on the received segments.<\/span><\/p>\n<pre class=\"lang:php decode:true\">protected function matchAction(\\Magento\\Framework\\App\\RequestInterface $request, array $params)\r\n{\r\n&nbsp;&nbsp;$moduleFrontName = $this-&gt;matchModuleFrontName($request, $params['moduleFrontName']);\r\n&nbsp;&nbsp;if (empty($moduleFrontName)) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return null;\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;\/**\r\n&nbsp;&nbsp;&nbsp;* Searching router args by module name from route using it as key\r\n&nbsp;&nbsp;&nbsp;*\/\r\n&nbsp;&nbsp;$modules = $this-&gt;_routeConfig-&gt;getModulesByFrontName($moduleFrontName);\r\n&nbsp;&nbsp;if (empty($modules) === true) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return null;\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;\/**\r\n&nbsp;&nbsp;&nbsp;* Going through modules to find appropriate controller\r\n&nbsp;&nbsp;&nbsp;*\/\r\n&nbsp;&nbsp;$currentModuleName = null;\r\n&nbsp;&nbsp;$actionPath = null;\r\n&nbsp;&nbsp;$action = null;\r\n&nbsp;&nbsp;$actionInstance = null;\r\n&nbsp;&nbsp;$actionPath = $this-&gt;matchActionPath($request, $params['actionPath']);\r\n&nbsp;&nbsp;$action = $request-&gt;getActionName() ?: ($params['actionName'] ?: $this-&gt;_defaultPath-&gt;getPart('action'));\r\n&nbsp;&nbsp;$this-&gt;_checkShouldBeSecure($request, '\/' . $moduleFrontName . '\/' . $actionPath . '\/' . $action);\r\n&nbsp;&nbsp;foreach ($modules as $moduleName) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$currentModuleName = $moduleName;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$actionClassName = $this-&gt;actionList-&gt;get($moduleName, $this-&gt;pathPrefix, $actionPath, $action);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!$actionClassName || !is_subclass_of($actionClassName, $this-&gt;actionInterface)) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$actionInstance = $this-&gt;actionFactory-&gt;create($actionClassName);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;if (null == $actionInstance) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$actionInstance = $this-&gt;getNotFoundAction($currentModuleName);\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($actionInstance === null) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return null;\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$action = 'noroute';\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;\/\/ set values only after all the checks are done\r\n&nbsp;&nbsp;$request-&gt;setModuleName($moduleFrontName);\r\n&nbsp;&nbsp;$request-&gt;setControllerName($actionPath);\r\n&nbsp;&nbsp;$request-&gt;setActionName($action);\r\n&nbsp;&nbsp;$request-&gt;setControllerModule($currentModuleName);\r\n&nbsp;&nbsp;$request-&gt;setRouteName($this-&gt;_routeConfig-&gt;getRouteByFrontName($moduleFrontName));\r\n&nbsp;&nbsp;if (isset($params['variables'])) {\r\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request-&gt;setParams($params['variables']);\r\n&nbsp;&nbsp;}\r\n&nbsp;&nbsp;return $actionInstance;\r\n}\r\n<\/pre>\n<p><span style=\"font-weight: 400;\">Each segment in URL contains information for finding the necessary controller. Segments in URL can be represented as follows: <\/span><strong>moduleFrontName\/actionPath\/actionName<\/strong>,<strong>&nbsp;<\/strong>w<span style=\"font-weight: 400;\">here:<\/span><\/p>\n<ul>\n<li><b>moduleFrontName<\/b><span style=\"font-weight: 400;\">&nbsp;&mdash; modules front name as set in <\/span><b>routes.xml<\/b><span style=\"font-weight: 400;\"> file;<\/span><\/li>\n<li><b>actionPath<\/b><span style=\"font-weight: 400;\"> &mdash; the path to the controller;<\/span><\/li>\n<li><b>actionName<\/b><span style=\"font-weight: 400;\"> &mdash; the name of the controller.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">For URL like <\/span><strong>custom-module\/info\/product <\/strong><span style=\"font-weight: 400;\">t<\/span><span style=\"font-weight: 400;\">he path to the controller class might look like this:&nbsp;<\/span><strong>app\/code\/Vendor\/Custom_Module\/Controllers\/Info\/ProductController.php.<\/strong><\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-development\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><img src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/08\/magento-development-services.png\" width=\"481\" height=\"480\" alt=\"magento development services\" class=\"module_image\" title=\"Controllers, Routers and Responses in Magento 2\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Development Services<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento development<\/p><a href=\"https:\/\/belvg.com\/magento-development\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Click to visit the page<\/span><\/a><\/div><\/div><\/div>\n<h2>Responses<\/h2>\n<p><span style=\"font-weight: 400;\">The controller in Magento 2 can return several response types depending on the purpose and the necessary result.<\/span><\/p>\n<ul>\n<li><b>Page Result<\/b><span style=\"font-weight: 400;\"> (\\<\/span><i><span style=\"font-weight: 400;\">Magento\\Framework\\View\\Result\\Page<\/span><\/i><span style=\"font-weight: 400;\">) is the most common type of response. By returning this object, the controller starts the standard page rendering based on the corresponding XML layout handle.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">public function __construct(\r\n&nbsp;&nbsp;&nbsp;$pageFactory Magento\\Framework\\View\\Result\\PageFactory\r\n) {\r\n&nbsp;&nbsp;&nbsp;$this-&gt;pageResultFactory = $pageFactory\r\n}\r\npublic function execute()\r\n{\r\n&nbsp;&nbsp;&nbsp;return $this-&gt;pageResultFactory-&gt;create();\r\n}<\/pre>\n<ul>\n<li><b>JSON Result<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\Controller\\Result\\Json<\/span><\/i><span style=\"font-weight: 400;\">) allows you to return a response in JSON format. It can be used in API or AJAX requests.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">public function __construct(\r\n&nbsp;&nbsp;&nbsp;Magento\\Framework\\Controller\\Result\\JsonFactory $jsonResultFactory,\r\n) {\r\n&nbsp;&nbsp;&nbsp;$this-&gt;jsonResultFactory = $jsonResultFactory;\r\n}\r\npublic function execute()\r\n{\r\n&nbsp;&nbsp;&nbsp;$result = $this-&gt;jsonResultFactory();\r\n&nbsp;&nbsp;&nbsp;$class = new Class; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;$class-&gt;data = value;\r\n&nbsp;&nbsp;&nbsp;$result-&gt;setData($class);\r\n&nbsp;&nbsp;&nbsp;return $result; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\r\n}\r\n<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><b>Raw Result<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\Controller\\Result\\Raw<\/span><\/i><span style=\"font-weight: 400;\">) is used if you want to return a string to the browser without using Magento layout and view rendering.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">public function __construct(\r\n&nbsp;&nbsp;&nbsp;Magento\\Framework\\Controller\\Result\\Raw $rawResultFactory ,\r\n) {\r\n&nbsp;&nbsp;&nbsp;$this-&gt;rawResultFactory = $rawResultFactory;\r\n}\r\npublic function execute()\r\n{\r\n&nbsp;&nbsp;&nbsp;$result = $this-&gt;rawResultFactory-&gt;create();\r\n&nbsp;&nbsp;&nbsp;$result-&gt;setHeader('Content-Type', 'text\/xml');\r\n&nbsp;&nbsp;&nbsp;$result-&gt;setContents('&lt;root&gt;&lt;block&gt;&lt;\/block&gt;&lt;\/root&gt;);\r\n&nbsp;&nbsp;&nbsp;return $result;\r\n}<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><b>Forward Result<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\Controller\\Result\\Forward<\/span><\/i><span style=\"font-weight: 400;\">) allows to call another method\/controller without changing the URL or redirecting.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">public function __construct(\r\n&nbsp;&nbsp;&nbsp;Magento\\Framework\\Controller\\Result\\ForwardFactory $resultForwardFactory &nbsp;&nbsp;&nbsp;\r\n) {\r\n&nbsp;&nbsp;&nbsp;$this-&gt;resultForwardFactory = $resultForwardFactory;\r\n}\r\npublic function execute()\r\n{\r\n&nbsp;&nbsp;&nbsp;$result = $this-&gt;resultForwardFactory-&gt;create();\r\n&nbsp;&nbsp;&nbsp;$result-&gt;forward('noroute'); &nbsp;&nbsp;&nbsp;\r\n&nbsp;&nbsp;&nbsp;return $result;\r\n}<\/pre>\n<ul>\n<li style=\"font-weight: 400;\"><b>Redirect Result<\/b><span style=\"font-weight: 400;\"> (<\/span><i><span style=\"font-weight: 400;\">\\Magento\\Framework\\Controller\\Result\\Redirect<\/span><\/i><span style=\"font-weight: 400;\">) is used when a user needs to be redirected to a different URL.<\/span><\/li>\n<\/ul>\n<pre class=\"lang:php decode:true\">public function __construct(\r\n&nbsp;&nbsp;&nbsp;Magento\\Framework\\Controller\\Result\\RedirectFactory $resultRedirectFactory\r\n) {\r\n&nbsp;&nbsp;&nbsp;$this-&gt;resultRedirectFactory = $resultRedirectFactory;\r\n}\r\npublic function execute()\r\n{\r\n&nbsp;&nbsp;&nbsp;$result = $this-&gt;resultRedirectFactory-&gt;create();\r\n&nbsp;&nbsp;&nbsp;$result-&gt;setPath('*\/*\/index');\r\n&nbsp;&nbsp;&nbsp;return $result;\r\n}<\/pre>\n<p>We hope the article was helpful and led to better understanding of Request routing in Magento 2.<\/p>\n<p>Need expert <a href=\"https:\/\/belvg.com\/magento-development\" target=\"_blank\" rel=\"noopener\">Magento 2 development<\/a> for your online store? Turn to BelVG.<\/p>\n<div class=\"module_in_content_container\"><a href=\"https:\/\/belvg.com\/magento-webdesign\" class=\"link_to_module\" target=\"_blank\" rel=\"noopener\"><\/a><div class=\"module_content\"><img src=\"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2019\/01\/magento-webdesign-1.png\" width=\"100\" height=\"100\" alt=\"magento webdesign\" class=\"module_image\" title=\"Controllers, Routers and Responses in Magento 2\"><div class=\"module_content_part\"><h3 class=\"title\">Magento Webdesign<\/h3><p class=\"module_content_text\">Take your online store to the next level with BelVG Magento Webdesign<\/p><a href=\"https:\/\/belvg.com\/magento-webdesign\" class=\"link_2_to_module\" target=\"_blank\" style=\"display:inline-block\" rel=\"noopener\"><span class=\"read-more\">Visit the page<\/span><\/a><\/div><\/div><\/div>\n\n","protected":false},"excerpt":{"rendered":"Request routing in Magento 2","protected":false},"author":82,"featured_media":15492,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_acf_changed":false,"footnotes":""},"categories":[389,524],"tags":[],"class_list":["post-15477","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-magento2-development","category-magento-speed-optimization","post_format-post-format-image"],"acf":[],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.4 - aioseo.com -->\n\t<meta name=\"description\" content=\"Discover in-depth review of Request routing in Magento 2, including: Front controller, Controllers, Routers and Responses. Code extracts are provided.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Andrey Litvin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.4\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#article\",\"name\":\"Controllers, Routers & Responses in Magento 2 | BelVG Blog\",\"headline\":\"Controllers, Routers and Responses in Magento 2\",\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/Controllers-Routers-and-Responses-in-Magento-2.jpg\",\"width\":770,\"height\":433},\"datePublished\":\"2018-04-12T14:59:07+02:00\",\"dateModified\":\"2021-02-18T13:48:41+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":7,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#webpage\"},\"articleSection\":\"Magento 2 Development, Magento Speed Optimization, Image\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"name\":\"Magento 2 Development\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"position\":2,\"name\":\"Magento 2 Development\",\"item\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#listItem\",\"name\":\"Controllers, Routers and Responses in Magento 2\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#listItem\",\"position\":3,\"name\":\"Controllers, Routers and Responses in Magento 2\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/category\\\/magento2-development#listItem\",\"name\":\"Magento 2 Development\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin\",\"name\":\"Andrey Litvin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e1a6e3c80cb940fddb65016d94b015cd951b4a8a9cd7227565131f28ea95c7a0?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Andrey Litvin\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#webpage\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html\",\"name\":\"Controllers, Routers & Responses in Magento 2 | BelVG Blog\",\"description\":\"Discover in-depth review of Request routing in Magento 2, including: Front controller, Controllers, Routers and Responses. Code extracts are provided.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/author\\\/andrey-litvin#author\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/04\\\/Controllers-Routers-and-Responses-in-Magento-2.jpg\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html\\\/#mainImage\",\"width\":770,\"height\":433},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/controllers-routers-and-responses-in-magento-2.html#mainImage\"},\"datePublished\":\"2018-04-12T14:59:07+02:00\",\"dateModified\":\"2021-02-18T13:48:41+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/belvg.com\\\/blog\\\/\",\"name\":\"BelVG Blog\",\"description\":\"Magento and Prestashop articles\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/belvg.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Controllers, Routers & Responses in Magento 2 | BelVG Blog","description":"Discover in-depth review of Request routing in Magento 2, including: Front controller, Controllers, Routers and Responses. Code extracts are provided.","canonical_url":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#article","name":"Controllers, Routers & Responses in Magento 2 | BelVG Blog","headline":"Controllers, Routers and Responses in Magento 2","author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/04\/Controllers-Routers-and-Responses-in-Magento-2.jpg","width":770,"height":433},"datePublished":"2018-04-12T14:59:07+02:00","dateModified":"2021-02-18T13:48:41+02:00","inLanguage":"en-US","commentCount":7,"mainEntityOfPage":{"@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#webpage"},"isPartOf":{"@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#webpage"},"articleSection":"Magento 2 Development, Magento Speed Optimization, Image"},{"@type":"BreadcrumbList","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/belvg.com\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","name":"Magento 2 Development"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","position":2,"name":"Magento 2 Development","item":"https:\/\/belvg.com\/blog\/category\/magento2-development","nextItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#listItem","name":"Controllers, Routers and Responses in Magento 2"},"previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#listItem","position":3,"name":"Controllers, Routers and Responses in Magento 2","previousItem":{"@type":"ListItem","@id":"https:\/\/belvg.com\/blog\/category\/magento2-development#listItem","name":"Magento 2 Development"}}]},{"@type":"Organization","@id":"https:\/\/belvg.com\/blog\/#organization","name":"BelVG Blog","description":"Magento and Prestashop articles","url":"https:\/\/belvg.com\/blog\/"},{"@type":"Person","@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author","url":"https:\/\/belvg.com\/blog\/author\/andrey-litvin","name":"Andrey Litvin","image":{"@type":"ImageObject","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/e1a6e3c80cb940fddb65016d94b015cd951b4a8a9cd7227565131f28ea95c7a0?s=96&d=mm&r=g","width":96,"height":96,"caption":"Andrey Litvin"}},{"@type":"WebPage","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#webpage","url":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html","name":"Controllers, Routers & Responses in Magento 2 | BelVG Blog","description":"Discover in-depth review of Request routing in Magento 2, including: Front controller, Controllers, Routers and Responses. Code extracts are provided.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/belvg.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#breadcrumblist"},"author":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"creator":{"@id":"https:\/\/belvg.com\/blog\/author\/andrey-litvin#author"},"image":{"@type":"ImageObject","url":"https:\/\/belvg.com\/blog\/wp-content\/uploads\/2018\/04\/Controllers-Routers-and-Responses-in-Magento-2.jpg","@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html\/#mainImage","width":770,"height":433},"primaryImageOfPage":{"@id":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html#mainImage"},"datePublished":"2018-04-12T14:59:07+02:00","dateModified":"2021-02-18T13:48:41+02:00"},{"@type":"WebSite","@id":"https:\/\/belvg.com\/blog\/#website","url":"https:\/\/belvg.com\/blog\/","name":"BelVG Blog","description":"Magento and Prestashop articles","inLanguage":"en-US","publisher":{"@id":"https:\/\/belvg.com\/blog\/#organization"}}]}},"aioseo_meta_data":{"post_id":"15477","title":"Controllers, Routers &amp; Responses in Magento 2 | #site_title","description":"Discover in-depth review of Request routing in Magento 2, including: Front controller, Controllers, Routers and Responses. Code extracts are provided.","keywords":null,"keyphrases":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_custom_url":null,"og_image_custom_fields":null,"og_image_url":null,"og_image_width":null,"og_image_height":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_image_url":null,"twitter_title":null,"twitter_description":null,"schema_type":"default","schema_type_options":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null,"created":"2026-11-03 18:37:10","updated":"2026-11-03 18:37:10"},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/belvg.com\/blog\/category\/magento2-development\" title=\"Magento 2 Development\">Magento 2 Development<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">\u00bb<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tControllers, Routers and Responses in Magento 2\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/belvg.com\/blog\/"},{"label":"Magento 2 Development","link":"https:\/\/belvg.com\/blog\/category\/magento2-development"},{"label":"Controllers, Routers and Responses in Magento 2","link":"https:\/\/belvg.com\/blog\/controllers-routers-and-responses-in-magento-2.html"}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/15477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/users\/82"}],"replies":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/comments?post=15477"}],"version-history":[{"count":4,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/15477\/revisions"}],"predecessor-version":[{"id":25608,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/posts\/15477\/revisions\/25608"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media\/15492"}],"wp:attachment":[{"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/media?parent=15477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/categories?post=15477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/belvg.com\/blog\/wp-json\/wp\/v2\/tags?post=15477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}