Magento 2 – Redirected to Dashboard When Something is Wrong

errormagento2redirect

I'm trying to build a Magento 2 module that has an admin section.
But apparently I'm doing something wrong and when I click the admin menu link, I get redirected to the dashboard.
I've seen this question: Magento2 custom admin menu link is redirecting back to admin dashboard but that does not help much.
There is nothing in the logs, but for the moment, I don't care what the problem is.
What I need is a way to debug this.
Where is the actual redirect happening so I know where to start?

Best Answer

In app/bootstrap.php enable 'display_errors'

ini_set('display_errors', 1);

enable developer mode.

php bin/magento deploy:mode:set developer

Disable cache and check admin routes.xml file of your module frontname.

EDIT

It is redirect from Magento\Backend\App\AbstractAction

public function dispatch(\Magento\Framework\App\RequestInterface $request)
    {
        if (!$this->_processUrlKeys()) {
            return parent::dispatch($request);
        }

        if ($request->isDispatched() && $request->getActionName() !== 'denied' && !$this->_isAllowed()) {
            $this->_response->setStatusHeader(403, '1.1', 'Forbidden');
            if (!$this->_auth->isLoggedIn()) {
                return $this->_redirect('*/auth/login');
            }
            $this->_view->loadLayout(['default', 'adminhtml_denied'], true, true, false);
            $this->_view->renderLayout();
            $this->_request->setDispatched(true);
            return $this->_response;
        }

        if ($this->_isUrlChecked()) {
            $this->_actionFlag->set('', self::FLAG_IS_URLS_CHECKED, true);
        }

        $this->_processLocaleSettings();

        return parent::dispatch($request);
    }

In _processUrlKeys function redirecting from $this->_redirect($this->_backendUrl->getStartupPageUrl());

public function _processUrlKeys()
    {
        $_isValidFormKey = true;
        $_isValidSecretKey = true;
        $_keyErrorMsg = '';
        if ($this->_auth->isLoggedIn()) {
            if ($this->getRequest()->isPost()) {
                $_isValidFormKey = $this->_formKeyValidator->validate($this->getRequest());
                $_keyErrorMsg = __('Invalid Form Key. Please refresh the page.');
            } elseif ($this->_backendUrl->useSecretKey()) {
                $_isValidSecretKey = $this->_validateSecretKey();
                $_keyErrorMsg = __('You entered an invalid Secret Key. Please refresh the page.');
            }
        }
        if (!$_isValidFormKey || !$_isValidSecretKey) {
            $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
            $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
            if ($this->getRequest()->getQuery('isAjax', false) || $this->getRequest()->getQuery('ajax', false)) {
                $this->getResponse()->representJson(
                    $this->_objectManager->get(
                        'Magento\Framework\Json\Helper\Data'
                    )->jsonEncode(
                        ['error' => true, 'message' => $_keyErrorMsg]
                    )
                );
            } else {
                $this->_redirect($this->_backendUrl->getStartupPageUrl());
            }
            return false;
        }
        return true;
    }

Above is possible area to check. Maybe it is helpful.

[EDIT BY OP]
The method _processUrlKeys in the code above is the one that does the redirect.
And here is why.
the backend router, when it does not find a controller action to map the url on, it maps it to the 404 page (Magento\Backend\Controller\Adminhtml\Noroute\Index class).
then comes the _processUrlKeys method that checks the url key.
And this url key depends on the controller name. Since the key is generated based on the url module/controller/action it will not match against the key for the noroute action and $_isValidSecretKey will be false hence the redirect.
The redirect does not happen if the Add Secret Key to URLs is off in the config panel. In this case you get a simple 404 page.

Related Topic