8

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?

0

3 Answers 3

19

You created an infinite loop:

  1. you request a URL starting with "test"
  2. your router sets module, controller and action to "moduletest", "test", and "test"
  3. you forward, using this request (the URL is still starting with "test")
  4. 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;
    }

    ...
2
  • 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 URL
    – Jackson
    Commented 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
    Commented Dec 27, 2018 at 6:29
10

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.

9

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.

4
  • 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
    Commented Dec 27, 2018 at 5:37
  • also run the upgrade and compile command Commented Jan 4, 2021 at 20:22
  • This is still true for Magento 2.4.3 Commented 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. Commented Feb 10, 2023 at 13:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.