3

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.

5

5 Answers 5

1

Thanks everyone.

At the end, I deleted my module completely and I did it all over again, and it works. I still do not know that I would have had a bad with the original module.

Thanks for your time.

0

Your execute method has no return value. It should be:

$page = $this->pageFactory->create();
return $page;
2
  • Thanks for your answer. Originally I have that, but I changed for test if execute() method was reached, but not... Feb 22, 2017 at 21:07
  • Try putting a trailing slash on your menu actions: morwi_mobileapp/apporder/. Feb 22, 2017 at 21:48
0

There is no controller file so it will take you to dashboard page

You need to create controller same for Appreport, You have only created Apporder that is working fine. create controller for Appreport.

1
  • Hi, I edited my question. I have also the Appreport controller, but the content is Is quite similar to the other. Both controllers redirect me to dashboard. Feb 23, 2017 at 14:43
0

Take a look your menu.xml, there are two action controllers:

  • morwi_mobileapp/appreport
  • morwi_mobileapp/apporder

1) app/code/Morwi/MobileApp/Controller/Adminhtml/Appreport/Index.php for morwi_mobileapp/appreport. You're missing this controller.

2) app/code/Morwi/MobileApp/Controller/Adminhtml/Apporder/Index.php for morwi_mobileapp/apporder

EDIT:

Your menu.xml and routes.xml should be under etc/adminhtml folder.

2
  • Hi, I edited my question. I have also the Appreport controller, but the content is Is quite similar to the other. Both controllers redirect me to dashboard. Feb 23, 2017 at 14:42
  • Did you clear Magento Cache and delete var/generation? I checked on my local machine it worked fine. Can you post your folder structure? Feb 23, 2017 at 15:23
0

This happens because 'secret key' is missing when you just type in URL manually. Secret key feature is enabled by default and can be disabled here:

Stores => Configuration => Advanced => Admin => Security => Add Secret Key to URLs

. Then you should be able to reach your action.

1
  • this is a useful workaround, but production-ready code should be able to be ran using this feature. Dec 30, 2019 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.