<type name="Magento\Framework\App\RouterList">
<arguments>
<argument name="routerList" xsi:type="array">
<item name="routingExample" xsi:type="array">
<item name="class" xsi:type="string">ExampleCorp\RoutingExample\Controller\Router</item>
<item name="disable" xsi:type="boolean">false</item>
<item name="sortOrder" xsi:type="string">40</item>
</item>
</argument>
</arguments>
</type>
Creating the controller that will handle the routing
route and will get the parameters passed by our router.
File: ExampleCorp/RoutingExample/Controller/Index/Index.php
<?php
declare(strict_types=1);
namespace ExampleCorp\RoutingExample\Controller\Index;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
/**
* 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, that will match the custom route name learning
with the existing routing
route.
File: ExampleCorp/RoutingExample/Controller/Router.php
<?php
declare(strict_types=1);
namespace ExampleCorp\RoutingExample\Controller;
use Magento\Framework\App\Action\Forward;
use Magento\Framework\App\ActionFactory;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\RouterInterface;
/**
* 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;
}
}
As a result, by accessing the http://site.com/learning route, the http://site.com/routing/index/index route is loaded.