1

I'm following the MagentoU course of basic development(full of errors...) and in some point I must create a controller that allows acces only if the GET parameter secret is set.

I configure the etc/adminhtml/routes.xml as follows:

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

And then make the controller Controller\Adminhtml\Action\Index.php

<?php
namespace Training\Test\Controller\Adminhtml\Action;

class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $content = $this->getRequest()->getParam('content');
        $this->getResponse()->appendBody($content);
    }

    protected function _isAllowed()
    {
        $secret = $this->getRequest()->getParam('secret');
        return isset($secret);
    }
}

The thing is, when I call the controller with base_url/admin/test/action/index/content/hello it prints hello on the screen without check the _isAllowed() function.

I'm missing something?

1

Admin controller should extend from \Magento\Backend\App\Action. So replace your code following code

namespace Training\Test\Controller\Adminhtml\Action;

class Index extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $content = $this->getRequest()->getParam('content');
        $this->getResponse()->appendBody($content);
    }

    protected function _isAllowed()
    {
        $secret = $this->getRequest()->getParam('secret');
        return isset($secret);
    }
}
  • Thanks for your fast response Sohel. But with this change now ins't printing anything, it redirects me to the dashboard (even with the param secret in the url). – Adrià Garrido Mar 13 '17 at 15:16
  • Right, So you need to create a menu for it OR Stores -> Configuration -> ADVANCED -> Admin -> Security -> Add Secret Key to URLs set no – Sohel Rana Mar 13 '17 at 15:29

Your Answer

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

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