This article is about how to handle changing your module’s url by default.
normally we create a new page in our module which will have form Magento/Demo/Cotroller/Index/Index.php
generated url in file Magento/Demo/etc/frontend/routes.xml , called via block to .xml file of form Magento/Demo/view/frontend/layout/demo_index_index.xml . Sometimes some clients don’t want to use the existing url in the module and want an existing router change that you make without spending too much time. Here is how I share that you can refer to do it.
First add the Routes.php file to the Controller folder, then replace this code and edit it to match your requirements.
<?php
namespace Magento\Demo\Controller;
class Routes implements \Magento\Framework\App\RouterInterface
{
protected $actionFactory;
protected $_response;
public function __construct(
\Magento\Framework\App\ActionFactory $actionFactory,
\Magento\Framework\App\ResponseInterface $response,
)
{
$this->actionFactory = $actionFactory;
$this->_response = $response;
}
public function match(\Magento\Framework\App\RequestInterface $request)
{
$identifier = trim($request->getPathInfo(), '/');
$router = "routes_demonew";
$urlSuffix = ".html";
if ($length = strlen($urlSuffix)) {
if (substr($identifier, -$length) == $urlSuffix) {
$identifier = substr($identifier, 0, strlen($identifier) - $length);
}
}
if ($identifier == $router) {
$request->setModuleName('demo')
->setControllerName('index')
->setActionName('index')
->setPathInfo('/demo/index/index');
return $this->actionFactory->create('Magento\Framework\App\Action\Forward');
}
}
}
our original routes as demo now change it to routes_demonew and you need to call this changer of Routes.php in the etc/frontend/di.xml file
<?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="demo" xsi:type="array">
<item name="class" xsi:type="string">Magento\Demo\Controller\Routes</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">22</item>
</item>
</argument>
</arguments>
</type>
</config>
good luck