0

I have created custom admin menu but when clicking on the menu it is redirecting to dashboard.

Here is the xml within 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="Cubet_RMA::rma" title="Cubet RMA" resource="Cubet_RMA::rma" module="Cubet_RMA"  sortOrder="20"/>
       <add id="Cubet_RMA::managerma" title="Manage RMA" resource="Cubet_RMA::Rmaadmin" parent="Cubet_RMA::rma" action="rmaadmin/rmaadmin/index" module="Cubet_RMA" sortOrder="20"/>
   </menu>
 </config>

Here is the xml within 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="rmaadmin" frontName="rmaadmin">
          <module name="Cubet_RMA"/>
       </route>
    </router>
</config>

Here is the Controller/Adminhtml/Rmaadmin/Index.php code:

<?php
 namespace Cubet\RMA\Controller\Adminhtml\Rmaadmin;
 class Index extends \Magento\Backend\App\Action
{

const ADMIN_RESOURCE = 'ListReturns';

protected $resultPageFactory;
public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory)
{
    $this->resultPageFactory = $resultPageFactory;
    parent::__construct($context);
}


public function execute()
{
   $return = $this->resultPageFactory->create();
   // $return->setActiveMenu('Cubet_RMA::rma');
   // $return->getConfig()->getTitle()->prepend((__('Manage RMA Requests')));
   return $return;
}
}

Please help me on this.

6 Answers 6

3

You can try the below code.

I tried the code in my local machine. It was working perfectly.

Step1 - Creating registration.php in the following path app/code/Cubet/RMA

<?php

    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Cubet_RMA',
        __DIR__
    );

Step2 - Creating module.xml in the following path app/code/Cubet/RMA/etc

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Cubet_RMA" setup_version="1.0.2">
        <sequence>
            <module name="Magento_Backend" />
        </sequence>
    </module>
</config>

Step3 - Creating routes.xml in the following path app/code/Cubet/RMA/etc/adminhtml

<?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="rmaadmin" frontName="rmaadmin">
            <module name="Cubet_RMA" before="Magento_Backend" />
        </route>
    </router>
</config>

Step4 - Creating menu.xml in the following path app/code/Cubet/RMA/etc/adminhtml

<?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>

        <update id="Cubet_RMA::CubetRMA" title="RMA" resource="Cubet_RMA::Rma" module="Cubet_RMA" sortOrder="300" />
        <add id="Cubet_RMA::rma" title="RMA" module="Cubet_RMA" sortOrder="50" resource="Cubet_RMA::Rmaadmin" parent="Cubet_RMA::CubetRMA" action="rmaadmin/index" />

    </menu>
</config>

Step5 - Creating Index.php in the following path /var/www/html/Magento-CE-2.1.6/app/code/Cubet/RMA/Controller/Adminhtml/Index

<?php

namespace Cubet\RMA\Controller\Adminhtml\Index;

use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Backend\App\Action
{
    /**
     * @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()
    {
        echo "welcome"; exit;

        /** @var \Magento\Backend\Model\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }
}

Run Setup Upgrad.

Clear Cache.

Give folder permission.

2

For me it was the isallowed method:-

protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Companyname_Module::controllername');
    }
0

After checking on Magento 2.1.7, your code will work. However, to make the current code better, we need to make some changes:

Create ACL:

app/code/Cubet/RMA/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>
        <resource id="Magento_Backend::admin">
            <resource id="Cubet_RMA::rma">
                <resource id="Cubet_RMA::managerma" title="Manager RMA" translate="title" sortOrder="30"/>
            </resource>
        </resource>
    </acl>
</config>

Set const ADMIN_RESOURCE: the resource id in acl.xml. For example:

const ADMIN_RESOURCE = 'Cubet_RMA::rma';
2
  • Thanks Khoa TruongDinh! it was the version issue I guess. Like you mentioned it is working on 2.1.7. You saved my day! Commented Jul 13, 2017 at 5:41
  • In your case, seem that you need to create acl.xml and change the constant const ADMIN_RESOURCE = 'Cubet_RMA::rma';. And then, check again. Commented Jul 13, 2017 at 5:43
0

That is because Magento use “secret key” for each request on the adminhtml. You only need to disable that option from the backend.

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

This works for me

Source:

https://www.codealist.net/magento-2-custom-admin-controller-action-redirected-to-dashboard/

0

Disable the use of "secret key" in the backend would be a security risk. Instead, add the following to your Index class.

    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    const ADMIN_RESOURCE = 'Cubet_RMA::rma';

    /**
     * Array of actions which can be processed without secret key validation
     *
     * @var string[]
     */
    protected $_publicActions = ['view', 'index'];

If you check the code in vendor/magento/module-sales/Controller/Adminhtml/Order.php, you will see that's how it is done.

0

Create a New Registration.php navigate to the file in Magento2Root/app/code/Cloudways/Adminmenu/ and add the code below to it:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'Cloudways_Adminmenu',
   __DIR__
);

Create a Module.xml Create a module.xml file in Cloudways/Adminmenu/etc/ with the following code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Cloudways_Adminmenu" setup_version="1.0.0"></module>
</config>

Declare Route you need to create the new file named routes.xml in the path app\code\Cloudways\Adminmenu\etc\routes.xml

I added the routes.xml file in the folder named admin html, because I want these codes only to apply to the admin.

?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:app/etc/routes.xsd">
  <route id="admin">
  <route id="admin" frontName=”mainmenu”>
  <module name="Cloudways_Adminmenu"/>
</router>
</router>
</config>

Create a New Menu Item Create a new file menu.xml in Cloudways/Adminmenu/etc/adminhtml/ and add the following content to it:

<?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="Cloudways_Adminmenu::first_level_demo"
            title="Cloudways Admin Menu”
module="Cloudways_Adminmenu"
sortOrder="20"
resource="Magento_Backend::content" />
       <add id="Cloudways_Adminmenu::second_level_demo"
            title="Sub Menu”
module="Cloudways_Adminmenu"
sortOrder="1"
action="menuitem/index/index"
parent="Cloudways_Adminmenu::first_level_demo"
            resource="Magento_Backend::content" />
  </menu>
</config>

Clear the Magento Cache!

rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/*
php bin/magento cache:clean
php bin/magento cache:flush
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex

Your Answer

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

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