Skip to main content

How to create custom router in Magento 2?

In this blog, we will discuss Magento routing and how to create a custom router for any existing router. The route defines the name of the module which is responsible for the requested URL. 

The FrontController class searches through a list of routers, provided by the RouterList class until it matches one that can process a request. When the FrontController finds a matching router, it dispatches the request to an action class returned by the router.

Standard Router

For example, if URL http://localhost/demo/request/path is called from the browser then it defines demo as the module_name/front_name, request as the controller_name & path as the action_name. 

When we create a new module, routes are defined in the routes.xml file maps which module to use for a URL with a specific frontName and area. The location of the routes.xml file in a module, either etc/frontend or etc/adminhtml, specifies where those routes are active.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="demonstration" frontName="demo">
            <module name="Vendor_module"/>
        </route>
    </router>
</config>

Where:

  • standard - specifies the name of the router in Magento. See the reference tables in the Router class section. (Standard is used for frontend routes.)
  • demonstration - specifies the unique node id for this route in Magento, and is also the first segment of its associated layout handle XML filename (routeId_controller_action.xml).
  • demo - specifies the first segment after the base URL of a request.
  • Vendor_module - specifies the name of your module.

Custom Router

Step 1: To create a router first you need to add it to the \Magento\Framework\App\RouterList, which is transferred to the Front Controller and contains all the available routers in the right order. To do this, we use the di.xml file in our module.

app/code/Vendor/Module/etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/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\CustomRouter</item>
                    <item name="disable" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">40</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

Step 2: After this, we need to create a customRouter class implementing RouterInterface and define the match() function in this class to use your own matching logic.

<?php
namespace Vendor\Module\Controller;
 
class CustomRouter implements \Magento\Framework\App\RouterInterface
{
    /**
     * @var \Magento\Framework\App\ActionFactory
     */
    protected $actionFactory;
 
    /**
     * Response
     *
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;
 
    /**
     * @param \Magento\Framework\App\ActionFactory $actionFactory
     * @param \Magento\Framework\App\ResponseInterface $response
     */
    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_response = $response;
    }
 
    /**
     * Validate and Match
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return bool
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');
        if($identifier == 'inscription') {
            $request->setModuleName('customer');
            $request->setControllerName('account');
            $request->setActionName('create');
        } else {
            return;
        }
 
        /*
         * We have match and now we will forward action
         */
        return $this->actionFactory->create('Magento\Framework\App\Action\Forward', ['request' => $request]);
    }
}

Above example is used to redirect the customer registration page to new URL.

Step 3: After this, we need to create our routes.xml. If we want to override or extend routes of an existing module then we can add before or after parameters in the module entry.

<?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="demonstration" frontName="demo">
            <module name="Vendor_Module"/>
        </route>
        <route id="customer">
            <module name="Vendor_Module" before="Magento_Customer" />
        </route>
    </router>
</config>

Step 4: Finally we need to create an action class that implements ActionInterface that router returns on the matched requests. 

<?php
declare(strict_types=1);

namespace Vendor\Module\Controller\Request;

use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;

/**
 * Class Index
 */

class Path implements HttpGetActionInterface
{
    /**
     * @var ForwardFactory
     */
    private $forwardFactory;

    /**
     * @param ForwardFactory $forwardFactory
     */
    public function __construct(
        ForwardFactory $forwardFactory
    ) {
        $this->forwardFactory = $forwardFactory;
    }

    /**
     * @inheritdoc
     */
    public function execute()
    {
        /** @var Forward $forward */
        $forward = $this->forwardFactory->create();
        return $forward->forward('defaultNoRoute');
    }
}

This approach can be used if you want to translate the URL into your required language as well. 

Comments

Popular posts from this blog

Unlocking Success: The Vital Role of the Contact Us Page in E-commerce

In the dynamic realm of e-commerce, where digital transactions reign supreme, the significance of customer communication cannot be overstated. Amidst the plethora of factors influencing the success of an online store, one often overlooked yet fundamentally important element is the Contact Us page. This seemingly humble corner of a website holds immense power, serving as a linchpin in fostering trust, resolving issues, and nurturing customer relationships. Let's delve deeper into why the Contact Us page is not just an afterthought but a strategic asset for e-commerce businesses, backed by proven data. Building Trust and Credibility Trust is the cornerstone of any successful e-commerce venture. According to a survey conducted by Edelman, 81% of consumers say that trusting a brand to do what is right is a deciding factor in their purchasing decisions. A prominently displayed Contact Us page with clear contact information, including a physical address, phone number, and email address, ...

Magento - LogRocket Integration

In today’s competitive eCommerce landscape, understanding user behavior is crucial for optimizing customer experiences and improving conversion rates. Magento 2, a powerful and flexible eCommerce platform, allows merchants to customize their online stores extensively. However, monitoring how users interact with these customizations is often challenging. This is where LogRocket, a modern session replay tool, comes into play. Integrating LogRocket with Magento 2 can provide invaluable insights into user behavior, performance bottlenecks, and UX issues. In this blog post, we’ll walk you through the steps to integrate LogRocket with Magento 2, and how this integration can help you improve your store’s performance and user experience. What is LogRocket? LogRocket is a session replay tool that enables you to record and playback user activity on your website. It tracks interactions such as clicks, scrolls, and form inputs, giving you a clear view of how users navigate your store. In addition,...

Missing crypt key for upgrading Magento

This is my first experience setting up a local docker environment for my project which is in Magento 2 Cloud edition. While doing setup by following Magento docs, I got stuck in the step "Deploy adobe commerce in the Docker Container". Here I was facing the issue: "Missing crypt key for upgrading Magento". Reason : I have taken a DB backup from my staging environment, it has a crypt_key stored which is used by Magento for encrypting the secured data. Solution : If you are not using docker for local setup then, you can simply update the crypt_key value in the app/etc/env.php file. But if you are using docker for your local setup, then you need to include your crypt_key in the config.php inside the .docker folder in your Magento root directory. Open the  .docker/config.php file. Then, search for the key MAGENTO_CLOUD_VARIABLES and add your crypt_key like below: Once you add the crypt_key in the file, try to again execute the  docker-compose run --rm deploy cloud-d...