Back to Top

Custom router in Magento2

Updated 6 May 2022

In this post, you will learn how to create a custom router in magento2.

To Create the Router first, need to add our custom route into the \Magento\Framework\App\RouterList class.

For this, we use the di.xml file in our module.

Check the Following path: app/code/Vendor/Module/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\App\RouterList">
        <argument name="routerList" xsi:type="array">
            <item name="customRoute" xsi:type="array">
                <item name="class" xsi:type="string">Vendor\Module\Controller\Router</item>
                <item name="disable" xsi:type="boolean">false</item>
                <item name="sortOrder" xsi:type="string">40</item>

After that, now we need to create a custom router class.

Searching for an experienced
Magento 2 Company ?
Find out More

It will match the custom route name learning with the existing routing route.

Check the Following path: app/code/Vendor/Module/Controller/Router.php:


namespace Vendor\Module\Controller;

use Magento\Framework\App\Action\Forward;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\RouterInterface;

 * Class Router
class Router implements RouterInterface
     * @var ActionFactory
    private $actionFactory;

     * @var ResponseInterface
    private $response;

     * Router constructor.
     * @param ActionFactory $actionFactory
     * @param ResponseInterface $response
    public function __construct(
        ActionFactory $actionFactory,
        ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->response = $response;

     * @param RequestInterface $request
     * @return ActionInterface|null
    public function match(RequestInterface $request): ?ActionInterface
        $identifier = trim($request->getPathInfo(), '/');

        if (strpos($identifier, 'learning') !== false) {
                'first_param' => 'first_value',
                'second_param' => 'second_value'

            return $this->actionFactory->create(Forward::class, ['request' => $request]);

        return null;

Now, we need to create our routes.xml file under:


<?xml version="1.0"?>

<config xmlns:xsi=""
    <router id="standard">
        <route id="routing" frontName="routing">
            <module name="Vendor_Module" />

Let’s check the above route as an example, Declaring the layout handler for our new route:


<?xml version="1.0"?>

<page xmlns:xsi=""
        <referenceBlock name="page.main.title">
            <action method="setPageTitle">
                <argument translate="true" name="title" xsi:type="string">Custom Routing Page</argument>

Creating the controller that will handle the routing route and will get the parameters passed by our router.



namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

 * Class Index
class Index implements HttpGetActionInterface
     * @var PageFactory
    private $pageFactory;

      * @var RequestInterface
    private $request;

     * @param PageFactory $pageFactory
     * @param RequestInterface $request
    public function __construct(PageFactory $pageFactory, RequestInterface $request)
        $this->pageFactory = $pageFactory;
        $this->request = $request;

     * @inheritdoc
    public function execute()
        // Get the params that were passed from our Router
        $firstParam = $this->request->getParam('first_param', null);
        $secondParam = $this->request->getParam('second_param', null);

        return $this->pageFactory->create();

As a result, accessing the route, the route is loaded.


That’s it.

Happy Coding !!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home