Вопрос

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!

Это было полезно?

Решение

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top