Magento2 Admin Controller – Fix Custom Admin Controller Redirect Issue

admin-controllermagento2router

I've created custom extension in the admin panel under the menu of products. The problem is that I can't get the controller working. Code as fallows:

in etc/adminhtml/menu.xml:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
    <menu>
        <add id="Vendor_Gift::Gift" title="Gifts" module="Vendor_Gift" parent="Magento_Catalog::inventory" action="gift/gift/" resource="Vendor_Gift::gift"/>
    </menu>
</config>

-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="gift" frontName="gift">
            <module name="Vendor_Gift" />
        </route>
    </router>
</config>

-in Vendor/Gift/Controller/Adminhtml/Gift/Index.php

<?php
namespace   Vendor\Gift\Controller\Adminhtml\Gift;

class Index extends \Magento\Backend\App\Action
{
    public function execute()
    {
        die('dd');
    }
}

The generated link in the menu Gifts is:
http://77.81.188.106/admin/gift/gift/index/key/bc29dbd2656f4c8c13599c4c63e1485afd9ffc3440fe4f9d9ceec8171b03d87a/

I just don't get where am I mistaken, is it in the menu link tab .. ?

Best Answer

  1. Double check to make sure all your files, folders and namespace are correct.

  2. Did you include the following files:

    • /Gift/registration.php
    • /Gift/composer.json
    • /Gift/etc/module.xml

Using the instruction you provide, I have recreated your module and it work find see demo https://github.com/magepal/stackexchange/tree/develop/102629

Edit

After do some research this is what I found. Once you copy a module to correct installation folder (i.e /app/code) it will automatically install (after running ./bin/magento setup:upgrade) regardless of if the folder structure matches the module name. The only think it care about is that the name in 'registration.php' and 'etc/module.xml' matches and all the different xmls will get merge (i.e menu)