I'm using Magento 2 CE Version 2.1.0
Taking reference of http://inchoo.net/magento-2/routing-in-magento-2/ for Routing.
My Router.php Controller Code
public function match(\Magento\Framework\App\RequestInterface $request) {
$identifier = trim($request->getPathInfo(), '/');
if (strpos($identifier, 'test') !== false) {
$request->setModuleName('moduletest')->setControllerName('test')->setActionName('test');
} else {
//There is no match
return;
}
return $this->actionFactory->create(
'Magento\Framework\App\Action\Forward', ['request' => $request]
);
}
I found @ vendor\magento\framework\App\FrontController.php
public function dispatch(RequestInterface $request)
{
\Magento\Framework\Profiler::start('routers_match');
$routingCycleCounter = 0;
$result = null;
while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
/** @var \Magento\Framework\App\RouterInterface $router */
foreach ($this->_routerList as $router) {
try {
$actionInstance = $router->match($request);
if ($actionInstance) {
$request->setDispatched(true);
$this->response->setNoCacheHeaders();
if ($actionInstance instanceof \Magento\Framework\App\Action\AbstractAction) {
$result = $actionInstance->dispatch($request);
} else {
$result = $actionInstance->execute();
}
break;
}
} catch (\Magento\Framework\Exception\NotFoundException $e) {
$request->initForward();
$request->setActionName('noroute');
$request->setDispatched(false);
break;
}
}
}
\Magento\Framework\Profiler::stop('routers_match');
if ($routingCycleCounter > 100) {
throw new \LogicException('Front controller reached 100 router match iterations');
}
return $result;
}
I have downloaded http://inchoo.net/magento-2/routing-in-magento-2/ gitHub code & installed & working fine. But it's not working for my custom module.
When i type http://localhost/magento2/mymodule/examplerouter it goes to InChoo Controller router not mine.
How to solve this issue?
3 Answers 3
You created an infinite loop:
- you request a URL starting with "test"
- your router sets module, controller and action to "moduletest", "test", and "test"
- you forward, using this request (the URL is still starting with "test")
Go to (1).
The inchoo article also explains that:
Forwarding means that it will break current routers loop and start the loop again
So, if you use forwarding, make sure that the request you forward to is not matched by the router again.
A possible solution for your case is to check if the request already has been modified:
public function match(\Magento\Framework\App\RequestInterface $request) {
if ($request->getModuleName() === 'moduletest') {
return;
}
...
-
I'm getting $request->getModuleName() BLANK. It's not returning any kind of module name. I'm accessing using localhost/magento2/mymodule/frontend-test where fontend-test is URL Identifier like CMS Page URLJackson– Jackson2016年09月13日 01:28:56 +00:00Commented Sep 13, 2016 at 1:28
-
@AnkitShah it's due to sortOrder issue in xml, now you have to accept the answer any of the following if you found helpful.Himanshu– Himanshu2018年12月27日 06:29:44 +00:00Commented Dec 27, 2018 at 6:29
If your code is perfect and you still get an error, Then you have to check your sortOrder of router plugin class in di.xml.
As magento official say : https://devdocs.magento.com/guides/v2.3/extension-dev-guide/routing.html
For frontend : enter image description here
For admin : enter image description here
Your sort order should be in between of magento standard router and default router sort order.
I know it's late to answer but it would be helpful for others.
In magento 2.1.10, the standard router sortOrder has been changed to 30, making the custom router (which sortOrder is 22) forward request infinite loop. Change the custom router order> 30 solves the issue.
-
Yes it's working. we have spend 3/4 hours debugging in it, But changing only sort order works. how magento can do this?! :(Himanshu– Himanshu2018年12月27日 05:37:35 +00:00Commented Dec 27, 2018 at 5:37
-
also run the
upgradeandcompilecommandVishalParkash– VishalParkash2021年01月04日 20:22:24 +00:00Commented Jan 4, 2021 at 20:22 -
This is still true for Magento 2.4.3Ben Tideswell– Ben Tideswell2022年03月12日 06:24:28 +00:00Commented Mar 12, 2022 at 6:24
-
After increasing the sort order greater than 30 i.e i have kept my custom router at 50 it is working now.Bharath Kumar– Bharath Kumar2023年02月10日 13:48:15 +00:00Commented Feb 10, 2023 at 13:48
Explore related questions
See similar questions with these tags.