I am trying to add a new Admin Menu in Magento 2 for first time. I followed these tutorials:
https://www.mageplaza.com/magento-2-module-development/create-admin-menu-magento-2.html
https://www.mageplaza.com/magento-2-module-development/create-admin-grid-magento-2.html
https://www.mageplaza.com/magento-2-module-development/how-to-create-controllers-magento-2.html
https://webkul.com/blog/create-admin-menu-and-controller-in-magento2/ http://excellencemagentoblog.com/blog/2016/04/10/magento2-admin-menu-route/
But right know, when click in my custom menu, it's redirecting me to dashboard, every time. I clear cache several time, logout and login again, but nothing.
I see this post: Magento 2 Custom Admin Action Redirected to Dashboard and says that it occurs for the absence of secret key, but the url in the menu have the secret key, but still redirects to dashboard.
Here are my files:
acl.xml
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Adminhtml::admin">
<resource id="Morwi_MobileApp::mobileapp" title="Mobile App" sortOrder="20">
<resource id="Morwi_MobileApp::mobileapp_appreport" title="Reportes de Servicio" sortOrder="10"></resource>
<resource id="Morwi_MobileApp::mobileapp_apporder" title="Ordenes de Surtido" sortOrder="20"></resource>
</resource>
</resource>
</resources>
</acl>
</config>
routes.xml
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
<router id="admin">
<route id="morwi_mobileapp" frontName="morwi_mobileapp">
<module name="Morwi_MobileApp" />
</route>
</router>
</config>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
<add id="Morwi_MobileApp::mobileapp" title="Mobile App" module="Morwi_MobileApp" sortOrder="70" resource="Morwi_MobileApp::mobileapp"/>
<add id="Morwi_MobileApp::mobileapp_appreport" title="Reportes de Servicio" module="Morwi_MobileApp" sortOrder="2" action="morwi_mobileapp/appreport" resource="Morwi_MobileApp::mobileapp_appreport" parent="Morwi_MobileApp::mobileapp"/>
<add id="Morwi_MobileApp::mobileapp_apporder" title="Ordenes de Surtido" module="Morwi_MobileApp" sortOrder="3" action="morwi_mobileapp/apporder" resource="Morwi_MobileApp::mobileapp_apporder" parent="Morwi_MobileApp::mobileapp"/>
</menu>
</config>
Index.php
<?php
namespace Morwi\MobileApp\Controller\Adminhtml\Apporder;
class Index extends \Magento\Backend\App\Action
{
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
echo __METHOD__;
exit;
}
}
I don't see nothing wrong. What could be happening?
Thanks.
EDIT 1:
I have also the Appreport controller, but I didn´t add this before, beacause the content is very the same of the other controller:
Index.php
<?php
namespace Morwi\Mobileapp\Controller\Adminhtml\Appreport;
class Index extends \Magento\Backend\App\Action
{
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
echo __METHOD__;
exit;
}
}
But both controllers redirect me to Dashboard.
EDIT 2:
I am starting think that is something wrong with my Module. I tried put my code in routes.xml file in other module (a Third party extension) and add the Index.php in that module, and it seems to work right. But, it is strange. If I run: php bin/magento module:status
my Module Morwi_Mobileapp
is listed as enabled, and is displayed en Admin Store > Configuration > Advanced > Advanced
. Also, it has a row in setup_module table. So, with this, it is safe to say that Magento is detecting my module, right?
But it is like Magento ingores my routes.xml file, or is not relating the frontName morwi_mobileapp with my module Morwi_Mobileapp for some reason.
morwi_mobileapp/appreport
. You're missing this controller. Take a look my answer.