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
Post a Comment