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.

1

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.

1

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';
  • Thanks Khoa TruongDinh! it was the version issue I guess. Like you mentioned it is working on 2.1.7. You saved my day! – Bridgit Thomas Jul 13 '17 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. – Khoa TruongDinh Jul 13 '17 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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