1

I have added a sample custom module in Magento 2. I have two controller function for admin panel page,

http://127.0.0.1/magento2/admin_magento2/sample/index/index/
http://127.0.0.1/magento2/admin_magento2/sample/sampletwo/index/

First url only display page. The second one displays 404 or redirecting to dashboard.

Please help me someone to solve this.

Note

I have added admin panel menu for first url only.

app\code\Test\Sample\Controller\Adminhtml\Index\Index.php

namespace Test\Sample\Controller\Adminhtml\Index;
class Index extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

app\code\Test\Sample\Controller\Adminhtml\Sampletwo\Index.php

namespace Test\Sample\Controller\Adminhtml\Sampletwo;
class Index extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

app\code\Test\Sample\etc\adminhtml\menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
    <menu>
        <add id="Test_Sample::parent" title="Sample" module="Test_Sample" sortOrder="100" resource="Test_Sample::parent"/>
        <add id="Test_Sample::index" title="Sample Index" module="Test_Sample" sortOrder="10" action="sample/index" resource="Test_Sample::index" parent="Test_Sample::parent"/>
    </menu>
</config>

app\code\Test\Sample\etc\adminhtml\routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="admin">
        <route id="sample" frontName="sample">
            <module name="Test_Sample" before="Magento_Adminhtml" />
        </route>
    </router>
</config>
6
  • app\code\Custom\Module\Controller\Adminhtml\Sampletwo\Index.php ?
    – Jackson
    Jan 6, 2017 at 7:11
  • yes that is another controller Jan 6, 2017 at 7:16
  • Please paste your controller code with path in question
    – Jackson
    Jan 6, 2017 at 7:17
  • yes edited please see the question Jan 6, 2017 at 7:22
  • 1
    show your routes.xml Jan 6, 2017 at 7:33

3 Answers 3

1

your action should be sample/sampletwo in menu.xml

13
  • Yes If I added it in menu it's working. But with adding menu in controller it redirects to dashboard Jan 6, 2017 at 7:33
  • show your routes.xml Jan 6, 2017 at 7:34
  • I added routes.xml in my question. Please see that Jan 6, 2017 at 7:37
  • change your action sample/sampletwo in menu.xml Jan 6, 2017 at 7:38
  • it will work @VigneshBala Jan 6, 2017 at 7:39
1

When attempting to access modules/functionality in the backend (Adminhtml), Magento checks for the presence of the secret Form Keys, and if/when someone does attempt to access an action and doesn't supply a valid FormKey and/or supplies an Invalid FormKey - the Adminhtml AbstractAction (Magento\Backend\App\AbstractAction) will route the user Admin UIX defined Startup page (which is usually the Dashboard page)

[This mechanism serves a "security checkpoint" to prevent users from accessing portions of the Backend that they don't have access to]


When accessing options from the Admin UIX Menu - Magento will automatically add the FormKey for you. That is why you are able to access the first URL v/s not able to access the second URL.


To prevent the check for FormKey you have two options:

(a) Turn off FormKey Validation

Navigation: Stores -> Settings : Configuration -> Advanced : Admin -> Security -> Setup "Add Secret Key to URLs" to "No"

It's very important to remember that doing so will turn off Secret Key Validation, and therefore may make your application vulnerable.

How To Turn Off FormKey Validation

(b) Turn off FormKey Validation for your controller

Specify the action(s) you wish to exempt, from the FormKey check, in the "$_publicActions" attribute of your Admin Controller.

protected $_publicActions = ['ENTER_NAME_OF_ACTION'];

Code Example:

class Index extends \Magento\Backend\App\Action
{
    protected $_publicActions = ['index'];

    public function execute()
    {
        echo "My Hello World! Controller";
    }

}
0

app\code\Test\Sample\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 frontName="test_sample" id="test_sample">
            <module before="Magento_Backend" name="Test_Sample"/>
        </route>
        <route frontName="sampletwo" id="sampletwo">
            <module before="Magento_Backend" name="Test_Sample"/>
        </route>
    </router>
</config>
1
  • I am not needed new frontname for every controller. I need to know why my controller redirects to dashbord. Jan 6, 2017 at 7:31

Your Answer

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

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