Reading list Switch to dark mode

    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="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\App\RouterList">
        <arguments>
            <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>
                </item>
            </argument>
        </arguments>
    </type>
    </config>

    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:

    <?php
    declare(strict_types=1);
    
    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) {
                $request->setModuleName('routing');
                $request->setControllerName('index');
                $request->setActionName('index');
                $request->setParams([
                    '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:

    app/code/Vendor/Module/etc/frontend/routes.xml

    <?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 id="routing" frontName="routing">
                <module name="Vendor_Module" />
            </route>
        </router>
    </config>

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

    Vendor/Module/view/frontend/layout/routing_index_index.xml

    <?xml version="1.0"?>
    
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="page.main.title">
                <action method="setPageTitle">
                    <argument translate="true" name="title" xsi:type="string">Custom Routing Page</argument>
                </action>
            </referenceBlock>
        </body>
    </page>

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

    Vendor/Module/Controller/Index/Index.php

    <?php
    declare(strict_types=1);
    
    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 http://site.com/learning route, the http://site.com/routing/index/index route is loaded.

    Screenshot-from-2022-05-06-13-11-36

    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