Magento 2 – Fix Custom Admin Page Too Many Redirects Issue

adminmagento2nginxredirect

I'm starting to get familiar with Magento 2.

I installed v2.3.1 in an Nginx/PHP-fpm and got it working, including the sample data.

Then I followed everything in the tutorial at https://devdocs.magento.com/guides/v2.3/ext-best-practices/extension-coding/example-module-adminpage.html

The menu shows up ok, but when I click the link generated in the menu:

adminlink/exampleadminnewpage/helloworld/index/key/(removed-key)/

I get the Too many redirects and tries to redirect to:

adminlink/exampleadminnewpage/helloworld/index/key/(removed-key)/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/adminhtml/dashboard

All other pages, frontend and backend, work fine.

EDIT:

I'm trying to debug this error and found that the the rewrite/redirect is not from Nginx it self.

In app/code/Magento/Backend/App/Request/BackendValidator.php, function validate (called from lib/internal/Magento/Framework/App/FrontController.php) when I try to access the new module the $action is instance of Magento\Framework\App\Action\Forward\Interceptor, which fails the test for not being instance of AbstractAction and start the redirect. But other pages (for example trying to access Content -> Pages) are instance of Magento\Cms\Controller\Adminhtml\Page\Index\Interceptor that pass that test.

EDIT 2:

In lib/internal/Magento/Framework/App/FrontController.php function processRequest the $this->requestValidator->validate($request, $actionInstance) throws an Exception "Invalid request received" for my custom module. And Null in other pages from Magento.

In lib/internal/Magento/Framework/App/Router/Base.php function matchAction $modules finds the correct module MyCompany_ExampleAdminNewPage and the cms page finds the Magento_Cms.

Then i found that in the same matchAction, magento didn't retrieve any actionList for the custom module. Which led me to conclude something should be wrong wih my app/code/MyCompany/ExampleAdminNewPage/Controller/Adminhtml/HelloWorld/Index.php. So i deleted and created a new one, checked for correct file permissions and ownership, and it solved my problem. Could also be the file encoding messing something up (wouldn't be the first, and won't be the last time this will happen).

Best Answer

I have added the code here. Please check and compare with your code. And do correction if need.

\app\code\MyCompany\ExampleAdminNewPage\registration.php

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'MyCompany_ExampleAdminNewPage',
        __DIR__
    );

\app\code\MyCompany\ExampleAdminNewPage\etc\module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
      <module name="MyCompany_ExampleAdminNewPage" setup_version="1.0.0">
      </module>
    </config>

\app\code\MyCompany\ExampleAdminNewPage\etc\adminhtml\menu.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
        <menu>
          <add id="MyCompany_ExampleAdminNewPage::greetings" title="Greetings" translate="title" module="MyCompany_ExampleAdminNewPage" parent="Magento_Backend::content" sortOrder="50" dependsOnModule="MyCompany_ExampleAdminNewPage" resource="MyCompany_ExampleAdminNewPage::greetings"/>
          <add id="MyCompany_ExampleAdminNewPage::greetings_helloworld" title="Hello World" translate="title" module="MyCompany_ExampleAdminNewPage" parent="MyCompany_ExampleAdminNewPage::greetings" sortOrder="10" dependsOnModule="MyCompany_ExampleAdminNewPage" action="exampleadminnewpage/helloworld" resource="MyCompany_ExampleAdminNewPage::greetings"/>
        </menu>
    </config>

\app\code\MyCompany\ExampleAdminNewPage\etc\adminhtml\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="admin">
          <route id="exampleadminnewpage" frontName="exampleadminnewpage">
              <module name="MyCompany_ExampleAdminNewPage"/>
          </route>
      </router>
  </config>

\app\code\MyCompany\ExampleAdminNewPage\Controller\Adminhtml\HelloWorld\Index.php

<?php
      namespace MyCompany\ExampleAdminNewPage\Controller\Adminhtml\HelloWorld;

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

        /**
         * Constructor
         *
         * @param \Magento\Backend\App\Action\Context $context
         * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
         */
        public function __construct(
            \Magento\Backend\App\Action\Context $context,
            \Magento\Framework\View\Result\PageFactory $resultPageFactory
        ) {
             parent::__construct($context);
             $this->resultPageFactory = $resultPageFactory;
        }

        /**
         * Load the page defined in view/adminhtml/layout/exampleadminnewpage_helloworld_index.xml
         *
         * @return \Magento\Framework\View\Result\Page
         */
        public function execute()
        {
             return  $resultPage = $this->resultPageFactory->create();
        }
      }
    ?>

\app\code\MyCompany\ExampleAdminNewPage\view\adminhtml\layout\exampleadminnewpage_helloworld_index.xml

<?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <head>
            <title>
                Greetings
            </title>
        </head>
        <body>
            <referenceContainer name="content">
                <block class="Magento\Backend\Block\Template" template="MyCompany_ExampleAdminNewPage::helloworld.phtml"/>
            </referenceContainer>
        </body>
    </page>

\app\code\MyCompany\ExampleAdminNewPage\view\adminhtml\templates\helloworld.phtml

<p>Hello World!</p>

Use above code I have installed and It's working fine.

Related Topic