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

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:

<?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:

<?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:

<?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!

Table of Contents