I'm looking to create a custom router so that if I go to www.mysite.com/promotions/cruise-trip
it would route to www.mysite/offers/promotions/index/id/5
for example. I'm not really sure how to do this though. I currently have the following set up:
etc/routes.xml
<?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 frontName="offers" id="offers">
<module name="Hurdo_Offers"/>
</route>
</router>
</config>
etc/di.xml
<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="customrouter" xsi:type="array">
<item name="class" xsi:type="string">Hurdo\Promotions\Controller\Router</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">22</item>
</item>
</argument>
</arguments>
</type>
</config>
Hurdo/Promotions/Controller/Router.php
<?php
namespace Hurdo\Promotions\Controller;
use \Magento\Framework\App\ActionFactory;
use \Magento\Framework\App\ResponseInterface;
use \Magento\Framework\App\RequestInterface;
class Router implements \Magento\Framework\App\RouterInterface
{
protected $actionFactory;
protected $_response;
public function __construct(
ActionFactory $actionFactory,
ResponseInterface $response
) {
$this->actionFactory = $actionFactory;
$this->_response = $response;
}
public function match(RequestInterface $request)
{
$identifier = trim($request->getPathInfo(), '/');
if (strpos($identifier, 'promotions') !== false) {
$request->setModuleName('offers')-> //module name
setControllerName('promotions')-> //controller name
setActionName('index')-> //action name
setParam('id', 5); //custom parameters
} else {
return false;
}
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward',
['request' => $request]
);
}
}
But I've no idea how to get this to work with a url key such as cruise-trip
etc.
Thanks guys for your help