Magento 2 Custom Admin Action Redirected to Dashboard – Fix

adminadmin-controllermagento2redirecturl-key

I am taking the Magento 2 fundamentals development course and the Admin Router / Controller exercise seems out of date. The router does work, but the controller does not, it always just re-routes to the admin homepage. Code for router app/code/Training/Test/etc/adminhtml/routes.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../vendor/magento/framework/App/etc/routes.xsd">
<router id="admin">
    <route id="test" frontName="test">
        <module name="Training_Test" before="Magento_Backend" />
    </route>
</router>
</config>

Code for Admin controller app/code/Training/Test/Controller/Adminhtml/Action/Index.php:

<?php

namespace Training\Test\Controller\Adminhtml\Action;
class Index extends \Magento\Backend\App\Action
{

public function execute()
    {
    die("test reached controller");
    }
protected function _isAllowed() {
    return true;
    }
}

When I go to the admin url of admin/test/action/index it just redirects and nothing happens. If I add a constructor and use xdebug, it shows that it reaches the controller constructor, but it never does the execute part. What am I missing?

Best Answer

This happens because 'secret key' is missing when you just type in URL manually. Secret key feature is enabled by default and can be disabled here: Stores => Configuration => Advanced => Admin => Security => Add Secret Key to URLs. Then you should be able to reach your action.

Related Topic