0

I have a module with a custom router.
Here's the code:

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="Module" xsi:type="array">
 <item name="class" xsi:type="string">Vendor\Module\Controller\Router</item>
 <item name="disable" xsi:type="boolean">false</item>
 <item name="sortOrder" xsi:type="string">22</item>
 </item>
 </argument>
 </arguments>
 </type>
</config>

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 id="testroute" frontName="testroute">
 <module name="Vendor_Module"/>
 </route>
 <route id="customrouter" frontName="customrouter">
 <module name="Vendor_Module"/>
 </route>
 </router>
</config>

Router.php

<?php
namespace Vendor\Module\Controller;
use Magento\Framework\Controller\ResultFactory;
class Router 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(), '/');
 if(strpos($identifier, 'customrouter') !== false) {
 $request->setModuleName('testroute')-> //module name
 setControllerName('productlist')-> //controller name
 setActionName('index')-> //action name
 setParam('param', 3); //custom parameters
 } else {
 return false;
 }
 return $this->actionFactory->create(
 'Magento\Framework\App\Action\Forward',
 ['request' => $request]
 );
 }
}

I have a breakpoint in the first line of the match() function: $identifier = trim($request->getPathInfo(), '/');

but this doesn't get triggered.

Instead the page is being loaded with an empty layout. (No 404 either)

I have a Index Controller in my module as well: Vendor/Module/Controller/Index/Index.php

If I set a breakpoint in that Controller's execute function, the program stops there when I call my customrouter URL https://storeurl.com/customrouter

Is the Index Controller the problem here because it is being used instead of the Router? Or is the Index Controller just being used because there is another problem with the custom router configuration?

How can I make this work?

asked May 6, 2020 at 10:02

1 Answer 1

0

Solved this by putting di.xml in app/code/Vendor/Module/etc/frontend/di.xml instead of app/code/Vendor/Module/etc/di.xml like some guides tell you to.
Maybe those are for older versions of Magento, or just wrong.

Also make sure to do a bin/magento setup:di:compile

answered May 6, 2020 at 14:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.