Magento – Magento 2 – Backend custom module 404 error

admin-controllermagento2modulePHPxml

I'm trying to create my custom module in the back-end, I followed a lot of tutorials, but I continue to get 404 error when I try to access to my controller. The only thing that work fine is the menu option, but when I go to http:/admin_host/macrocategorie/config/ I get Page not found 404

I have already tried to run this command:

  • php bin/magento setup:upgrade
  • php bin/magento setup:di:compile
  • php bin/magento cache:clean
  • php bin/magento cache:flush

But nothing change.

I have used below code :

Vendor\Module\registration.php

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

Vendor\Module\etc\module.xml

<?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="Vendor_Module" setup_version="2.0.0"/></config>

Vendor\Module\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="macrocategorie" frontName="macrocategorie">
        <module name="Vendor_Module" before="Magento_Backend"/>
    </route>
</router></config>

Vendor\Module\etc\adminhtml\menu.xml

<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Magento/Backend/etc/menu.xsd">
<menu>
    <add
            id="Vendor_Module::macrocategorie"
            title="Macrocategorie"
            module="Vendor_Module"
            sortOrder="100"
            parent="Magento_Backend::stores"
            resource="Vendor_Module::macrocategorie"
    />
    <add
            id="Vendor_Module::config"
            title="Configura Macrocategorie"
            module="Vendor_Module"
            sortOrder="105"
            parent="Vendor_Module::macrocategorie"
            action="macrocategorie/config/"
            resource="Vendor_Module::index"
    />
</menu></config>

Vendor\Module\Controller\Adminhtml\Config\Index.php

<?php namespace Vendor\Module\Controller\Adminhtml\Config; use Magento\Backend\App\Action\Context; use Magento\Framework\App\ResponseInterface; use Magento\Framework\View\Result\PageFactory; class Index extends \Magento\Backend\App\Action {

protected $resultPageFactory;

public function __construct(
    Context $context,
    PageFactory $resultPageFactory
)
{
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
}

/**
 * Dispatch request
 *
 * @return \Magento\Framework\Controller\ResultInterface|ResponseInterface
 * @throws \Magento\Framework\Exception\NotFoundException
 */
public function execute()
{
    die("Controller backend");
    $resultPage = $this->resultPageFactory->create();

    return $resultPage;
} }

Thank's for the support

Best Answer

It's probably because you're missing the ACL for your route.

You need to create etc/acl.xml with the following content:

<?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="Magento_Backend::stores">
                    <resource id="Vendor_Module::macrocategorie" title="Macrocategorie" sortOrder="80">
                        <resource id="Vendor_Module::index" title="Configura Macrocategorie" sortOrder="80">
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

And then you need to add the following to your action class:

protected function _isAllowed()
{
    return $this->_authorization->isAllowed('Vendor_Module::index');
}