0

I have referred below Article to display button in my Configuration page.

https://magecomp.com/blog/add-a-button-in-magento-2-stores-configuration-with-custom-action/

In file

app\code\Vendor\Extension\Block\System\Config\Button.php

there is a function where we can define our Ajax URL

public function getAjaxUrl()
    {
        return $this->getUrl(‘your action Url’);
    }

So for this, I want to create controller in Adminhtml which i can access using action url. but all the examples i found also creates menu in sidebar in addition to controller action.

For example this one: https://www.maximehuran.fr/en/admin-controller-creation-with-magento-2/

Is there any way to access controller action without creating menu?

Edit: After some modification to code, I can get into the controller using below URL:

my_backend_url/route_id/controller_name/index

but it redirects back me to Dashboard page.

As mentioned in this article:

Magento 2 Custom Admin Action Redirected to Dashboard

It's Secret Form Key issue.

We can get Secret form key only if we add controller to menu which i don't want to do!

Disabling Secret Form key is not what i prefer!

  • menu file is used to display menu in admin backend – Rk Rathod Jun 19 at 11:47
  • yes i know and i don't want to display in menu. so i have not created menu.xml file – Khushbu Jun 19 at 11:50
  • you can create controller without menu file but you can call controller using url – Rk Rathod Jun 19 at 11:54
  • yes but i am not able to call controller using url – Khushbu Jun 19 at 11:55
  • like this www.abc.com/admin/yout_route_id/controller_name – Rk Rathod Jun 19 at 12:04
0

Answer which worked for me is from this article:

Magento 2 Custom Admin Action Redirected to Dashboard

We can use protected $_publicActions = ['ENTER_NAME_OF_ACTION']; to turn off form key validation for specific controller like below:

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

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

}

It's better to disable form key for only my controller instead of everywhere in admin panel.

Please share if anyone has any better solution.

-5

Your namespace should be uppercase, not lowercase, if you want to go step by step How do I create a controller in magento 2

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.