Magento 2 admin menu action result Invalid security or form key. Please refresh the page. on click
Domanda
I have succesfully added a new admin menu. But the problem is whenever i clicked the admin menu button, I got Invalid security or form key. Please refresh the page.
Here're some of my codes.
This is my menu.xml
<add id="Testing_Basic::menu_item" title="Manage Items" module="Testing_Basic" sortOrder="10" parent="Testing_Basic::menu" action="Webpage/Create/Index" resource="Testing_Basic::menu_item"/>
And this is my directories looks like.
This is my
Controller/Adminhtml/Webpage/Index.php
<?php
/**
* Webkul Grid Controller
*
* @category Webkul
* @package Webkul_Grid
* @author Webkul Software Private Limited
*
*/
namespace Testing\Basic\Controller\Adminhtml\Webpage;
class Index extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $_resultPageFactory;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->_resultPageFactory = $resultPageFactory;
}
/**
* Grid List page.
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->_resultPageFactory->create();
$resultPage->setActiveMenu('Testing_Basic::grid_list');
$resultPage->getConfig()->getTitle()->prepend(__('Grid List'));
return $resultPage;
}
/**
* Check Grid List Permission.
*
* @return bool
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Testing_Basic::grid_list');
}
}
And this is my routes.xml
<?xml version="1.0"?>
<!--
/**
* Webkul_Grid route xml
*
* @category Webkul
* @package Webkul_Grid
* @author Webkul Software Private Limited
*
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="create" frontName="create">
<module name="Testing_Basic" />
</route>
</router>
</config>
Soluzione
add code in menu.xml
<add
id="Testing_Basic::item"
title="Manage Items"
module="Testing_Basic"
sortOrder="10"
parent="Testing_Basic::menu"
action="create/webpage/index"
resource="Testing_Basic::item"
/>
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 id="create" frontName="create">
<module name="Testing_Basic" />
</route>
</router>
</config>
and controler file path like
/Controller/Adminhtml/Webpage/Index.php
<?php
namespace Testing\Basic\Controller\Adminhtml\Webpage;
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Backend\App\Action;
class Index extends Action implements HttpGetActionInterface
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Index action
*
* @return \Magento\Backend\Model\View\Result\Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu('Testing_Basic::item');
$resultPage->addBreadcrumb(__('Grid List'), __('Grid List'));
$resultPage->addBreadcrumb(__('Manage Grid'), __('Manage Grid'));
$resultPage->getConfig()->getTitle()->prepend(__('Grid List'));
return $resultPage;
}
}
hope this help you. :)
Altri suggerimenti
It might help someone, Today, I faced the same issue Magento ver. 2.3.5 none of the above methods works for me: Then I checked the module controller and found this code :
/**
* {@inheritdoc}
*/
public function _isAllowed()
{
return $this->_authorization->isAllowed('Mymodule_Test::Test');
}
Solution: Update Users Role from admin and it will resolve the issue.
I had this issue in Magento 2.4.0. The reason was because the module folder was not correctly named. For example: the name should have been DemoModule and the incorrect name was ModuleDemo.