3

I created custom router. But, when I run it display error.

Front controller reached 100 router match iterations

di.xml :

<type name="Magento\Framework\App\RouterList">
    <arguments>
        <argument name="routerList" xsi:type="array">
            <item name="testRouter" 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">60</item>
            </item>
        </argument>
    </arguments>
</type>

Router.php :

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Module\Controller;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Router implements \Magento\Framework\App\RouterInterface
{
    /**
     * @var \Magento\Framework\App\ActionFactory
     */
    protected $actionFactory;

    /**
     * Event manager
     *
     * @var \Magento\Framework\Event\ManagerInterface
     */
    protected $_eventManager;

    /**
     * Store manager
     *
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;

    /**
     * News factory
     *
     * @var \Vendor\Module\Model\TestFactory
     */
    protected $_testFactory;

    /**
     * Config primary
     *
     * @var \Magento\Framework\App\State
     */
    protected $_appState;

    /**
     * Url
     *
     * @var \Magento\Framework\UrlInterface
     */
    protected $_url;

    /**
     * Response
     *
     * @var \Magento\Framework\App\ResponseInterface
     */
    protected $_response;

    /**
     * [__construct description]
     * @param \Magento\Framework\App\ActionFactory       $actionFactory [description]
     * @param \Magento\Framework\Event\ManagerInterface  $eventManager  [description]
     * @param \Magento\Framework\UrlInterface            $url           [description]
     * @param \Vendor\Module\Model\TestFactory           $testFactory   [description]
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager  [description]
     * @param \Magento\Framework\App\ResponseInterface   $response      [description]
     */
    public function __construct(
        \Magento\Framework\App\ActionFactory $actionFactory,
        \Magento\Framework\Event\ManagerInterface $eventManager,
        \Magento\Framework\UrlInterface $url,
        \Vendor\Module\Model\TestFactory $testFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\ResponseInterface $response
    ) {
        $this->actionFactory = $actionFactory;
        $this->_eventManager = $eventManager;
        $this->_url = $url;
        $this->_testFactory = $testFactory;
        $this->_storeManager = $storeManager;
        $this->_response = $response;
    }

    /**
     * Validate and Match Vendor Module and modify request
     *
     * @param \Magento\Framework\App\RequestInterface $request
     * @return \Magento\Framework\App\ActionInterface|null
     */
    public function match(\Magento\Framework\App\RequestInterface $request)
    {
        $identifier = trim($request->getPathInfo(), '/');

        $condition = new \Magento\Framework\DataObject(['identifier' => $identifier, 'continue' => true]);
        $this->_eventManager->dispatch(
            'vendor_module_controller_router_match_before',
            ['router' => $this, 'condition' => $condition]
        );
        $identifier = $condition->getIdentifier();

        if ($condition->getRedirectUrl()) {
            $this->_response->setRedirect($condition->getRedirectUrl());
            $request->setDispatched(true);
            return $this->actionFactory->create(\Magento\Framework\App\Action\Redirect::class);
        }

        if (!$condition->getContinue()) {
            return null;
        }

        /** @var \Vendor\Module\Model\Test $test */
        $test = $this->_testFactory->create();
        $testId = $test->checkIdentifier($identifier, $this->_storeManager->getStore()->getId());
        if (!$testId) {
            return null;
        }

        $request->setModuleName('test')->setControllerName('index')->setActionName('view')->setParam('test_id', $testId);
        $request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, $identifier);

        return $this->actionFactory->create(\Magento\Framework\App\Action\Forward::class);
    }
}

View.php :

<?php

namespace Vendor\Module\Controller\News;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class View extends Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Vendor\Module\Model\TestFactory
     */
    protected $_testFactory;

    /**
     * [__construct description]
     * @param Context                                    $context           [description]
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory [description]
     * @param \Vendor\Module\Model\TestFactory         $testFactory       [description]
     */
    public function __construct(
        Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Vendor\Module\Model\TestFactory $testFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->_testFactory = $testFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $id = $this->_request->getParam('test_id');
        $testColl = $this->_testFactory->load($id);
        $this->_view->loadLayout();
        $block = $this->_view->getLayout()->getBlock('testBlock');
        $block->setCatalogRule($testColl);
        $this->_view->renderLayout();
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__("Testing"));
    }
}

How to solve it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.