Magento – Magento 2 Why the admin route always redirect to the Dashboard

adminbackendmagento2

I have a custom module with a backend menu item, when i click on the link it always redirect me to the admin home page. These are my files:

app/code/Cosmo/Basemodule/Controller/Adminhtml/Base/Index.php

<?php
namespace Cosmo\Basemodule\Controller\Adminhtml\Base;

class Index extends \Magento\Backend\App\Action
{
    protected $resultPageFactory = false;
    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 'It works';
}

/*
 * Check permission via ACL resource
 */
protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Cosmo_Basemodule::base_manage_items');
}

}

app/code/Cosmo/Basemodule/etc/adminhtml/menu.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
<menu>
    <add id="Cosmo_Basemodule::base" title="Basemodule" module="Cosmo_Basemodule"
         sortOrder="50" resource="Cosmo_Basemodule::base"/>
    <add id="Cosmo_Basemodule::base_manage_items" title="Manage Items" module="Cosmo_Basemodule"
         sortOrder="50" parent="Cosmo_Basemodule::base" action="basemoduleadmin/base/index"
         resource="Cosmo_Basemodule::base_manage_items"/>
    <add id="Cosmo_Basemodule::base_configuration" title="Configuration" module="Cosmo_Basemodule"
         sortOrder="50" parent="Cosmo_Basemodule::base" action="adminhtml/system_config/edit/section/hello"
         resource="Cosmo_Basemodule::base_configuration"/>
</menu>

app/code/Cosmo/Basemodule/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="basemoduladmin" frontName="basemoduleadmin">
        <module name="Cosmo_Basemodule" />
    </route>
</router>

app/code/Cosmo/Basemodule/etc/acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
    <resources>
        <resource id="Magento_Backend::admin">
            <resource id="Cosmo_Basemodule::base" title="Basemodule" sortOrder="10" >
                <resource id="Cosmo_Basemodule::base_manage_items" title="Manage Items" sortOrder="0" />
                <resource id="Cosmo_Basemodule::base_configuration" title="Configuration" sortOrder="100" />
            </resource>
        </resource>
    </resources>
</acl>

Can you see the error?

Best Answer

Although this issue very old but we are also facing the same issue. after spending the day we come to know the exact root cause of the issue.

Basically, the issue is with routes.xml id & frontname missmatched. we observed that the issue gets resolved when we passed the exact name for id & frontname and created the layout.xml in for adminhtml.

In your case please check the routes.xml file as we can see there is spelling mismatch

so the corrected routes.xml is as below

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
    <route id="basemoduleadmin" frontName="basemoduleadmin">
        <module before="Magento_Backend" name="Cosmo_Basemodule" />
    </route>
</router>

Hope that something useful to others and save the time.