In Magento 2:
I Want to change this route/URL in myproject from http://myproject.com/style/index/index/parameter/paravalue
to http://myproject.com/style/parameter/paravalue
This is a custom Url
I have added the route in XML file i.e routes.xml
<route id="style" frontName="style">
<module name="modulename"/>
</route>
n i have a layout file style_index_index.xml where the template file is included.
/app/vendor/module/etc/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="style" xsi:type="array">
<item name="class" xsi:type="string">vendor\module\Controller\Style</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">22</item>
</item>
</argument>
</arguments>
</type>
</config>
Created the app/vendor/module/controller/Style.php
namespace vendor\module\Controller;
class Style 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(), '/');
$identifiers = explode('/', $$identifier);
if (strpos($identifier, 'style') !== false && count($identifiers) == 3) {
$request->setModuleName('style')->//module name
setControllerName('index')->//controller name
setActionName('index')->//action name
setParam($identifiers[1], $identifiers[2]); //custom parameters
} else {
return false;
}
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward', ['request' => $request]
);
}
}