Hello,
We are trying to create menu in admin panel in magento 2.4. We have added the menu successfully and configuration link of module is working fine. But when we add some other controller and its link, we are getting the error "Invalid security or form key. Please refresh the page.".
So please let us know how can we create customer controller and its link.
Thanks!
Hi @supportopeadb2 ,
You can create your custom controller with the below steps.
Here is the solution for creating a custom controller.
Step 1: Create a menu configuration file.
Step 2:After completing it. Clear the Magento cache and go to the admin panel.
Step 3: Create an admin controller file. you have to create a separate file for each action of admin in the Controller/Adminhtml folder in M2.
app/code/[vendor_name]/[module_name]/Controller/Adminhtml/[menu/submenu]/index.php
<?php namespace Milople\helloworld\Controller\Adminhtml\Employee; class Index extends \Magento\Backend\App\Action { /** * Hello test controller page. * * @return \Magento\Backend\Model\View\Result\Page */ public function execute() { echo __('Hello World.'); } /** * Check Permission. * * @return bool */ protected function _isAllowed() { return $this->_authorization->isAllowed('Hello::world'); } }
Step 4: Now, create a route configuration file for admin. app/code/[vendor_name]/[modle_name]/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="helloworld" frontName="helloworld"> <module name="milople_Hello" /> </route> </router> </config>
Now, your admin controller is ready.
If you are facing this error while adding menus then there are multiple reasons for this error to occur.
1. Check your module file for errors in the logs file. (search in the log files e.g. magento_root_directory/var/log/execption.log debug.log system.log)
2. Make sure that you have made the admin route.
3. Check your controller for any syntax error.
4. If you are making a grid then it is possible that there is any database connectivity error(records are missing).
or etc.
Check for these solutions and if you still need any help regarding this feel free to ask.