1

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

2 Answers 2

4
  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)

21
  • yes, except of composer.json but no where is it said that it is necessary. I can see that the module is added to app/etc/config.php withe the value of 1 Commented Feb 20, 2016 at 16:06
  • Did you get any error from ./bin/magento setup:upgrade? Commented Feb 20, 2016 at 16:10
  • 1
    thank you for the help anyway... I had given up on this... if you hadn't respond I probably would've found it tomorrow :) thank you! Commented Feb 20, 2016 at 16:54
  • 1
    This seem to be a issue in magento, I just rename the module to MagePal_Gift1 from MagePal_Gift (after it was enabled) and there was no errors on bin/magento setup:upgrade and end up with the same as you original had. Also there does not seem to be a way to uninstall this module ./bin/magento module:uninstall MagePal_Gift will result in MagePal_Gift is not an installed composer package even after change the name back to 'Gift' Commented Feb 20, 2016 at 17:41
  • 1
    You could create a github issue @ github.com/magento/magento2/issues Commented Feb 20, 2016 at 18:12
0

It turns out it was a combined problem - it was my fault and a flaw in Magento 2 module registration flaw.

When I first created my module I messed up the folder name of the module, so instead of the module being in Vendor/Gift it was in Vendor/Gitf.

After running

php bin/magento setup:upgrade 

without running the command

php bin/magento module:enable Vendor_Module

then the problem happened. Apparently, when u run the

php bin/magento setup:upgrade

it goes trough every folder in

app/code/<all_the_vendors_folders>/<all_the_vendors_modules>  

and if it find a file registration.php which registers a module, it probably only checks if the module name declared in etc/module.xml matches the one in the registration.php file. The result of that is that the module gets registered correctly but is placed in the wrong folder for which you have absolutely no information because php bin/magento setup:upgrade didn't give any errors and registered the module successfully. After that everything in my module code was correct, but since the module was in the folder "Gitf" instead of "Gift" nothing worked as expected, but also no errors were thrown it any point. So - for any new beginners, watch out I've spend like 3 hours looking for this... until I wrote here and a user confirmed my code is correct - then I was my fatal mistake, which was never announced to me by the system.

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.