In this blog, We will learn how to create a custom router in Magento2.
First of all, let’s discuss what is a router in Magento2.
A router is a PHP class that is used for matching and processing the URL request in the module.
In other words, Routing defines a name for a module that we use in the URL
to find the module and execute the controller action in Magento2.
Now, let’s create the custom routes, first of all, we need to declare our router.
To declare our route we have to create a routes.xml file on the following path:
<magento-root-dir.>/app/code/Webkul/Hello/etc/frontend/
Here we are taking an example, our vendor is Webkul and the module name is Hello
In the routes.xml file, we define a route by using the <route> tag. it contains the following attributes:
Id: id is a unique string it will identify the route. we will use this string to create our layout handler for the action.
frontName: frontName attribute is also a unique string that will be shown on the URL request.
<?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="Webkul_Hello" /> </route> </router> </config>
After creating routes.xml, now declare the layout handler for our new route.
To declare a layout handler we need to create a layout file routing_index_index.xml
on the following path.
<magento-root-dir.>/app/code/Webkul/Hello/view/frontend/layout/
<?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 Router Page</argument> </action> </referenceBlock> </body> </page>
After declaring the layout handler, now let’s add our custom route into the
MagentoFrameworkAppRouterList class.
To declare a custom route we have to create a di.xml file on the following path.
<magento-root-dir.>/app/code/Webkul/Hello/etc/frontend/
<type name="MagentoFrameworkAppRouterList"> <arguments> <argument name="routerList" xsi:type="array"> <item name="customRoute" xsi:type="array"> <item name="class" xsi:type="string">WebkulHelloControllerRouter</item> <item name="disable" xsi:type="boolean">false</item> <item name="sortOrder" xsi:type="string">40</item> </item> </argument> </arguments> </type>
After creating the di.xml file, now let’s create the controller
It will handle the routing route and will get the parameters passed by our router.
We have to create a controller class Index.php on the following path:
<magento-root-dir.>/app/code/Webkul/Hello/Controller/Index/
<?php declare(strict_types=1); namespace WebkulHelloControllerIndex; use MagentoFrameworkAppActionHttpGetActionInterface; use MagentoFrameworkAppRequestInterface; use MagentoFrameworkViewResultPage; use MagentoFrameworkViewResultPageFactory; /** * 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(); } }
In the end, let’s create the router class Router.php inside the <magento-root-dir.>/app/code/Webkul/Hello/Controller/ directory path.
that will match the custom route name ‘learning‘ with the existing ‘routing‘ route.
<?php declare(strict_types=1); namespace WebkulHelloController; use MagentoFrameworkAppActionForward; use MagentoFrameworkAppActionFactory; use MagentoFrameworkAppActionInterface; use MagentoFrameworkAppRequestInterface; use MagentoFrameworkAppResponseInterface; use MagentoFrameworkAppRouterInterface; /** * 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; } }
That’s all in this article, hope it will help you to create a custom router in magento2.
Try the above code and if you have any issues just comment below.
You can get more magento2 (Adobe Commerce) articles here.
Happy Coding 🙂
Source link
Leave a Reply