5

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

0

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');
}
  • In the Role Resources I could find my module but I still get the same error, 404 not found navigating to it. After adding the acl I also run the command php bin/magento cache:clean – 3tech - Alessandro Staffolani Oct 11 '16 at 14:17
0

you try to change namespace from "SaturTheme\Configuration" to "Vendor\Module" in your Controller and then clear cache

  • Well SaturTheme\Configuration is the real name of my vendor and my module, when I wrote this article I forgot to change it in namespace – 3tech - Alessandro Staffolani Oct 11 '16 at 16:16
  • Now I edited it – 3tech - Alessandro Staffolani Oct 11 '16 at 16:18
  • I tried to test your module and it working. I cleaned cache and deleted var/generation folder and run setup:di:compile. – User14000 Oct 12 '16 at 3:10
  • I tryed to remove and install again the module, now It works, I don't know why – 3tech - Alessandro Staffolani Oct 12 '16 at 12:46
0

Replace your Index.php controller file code.

<?php
namespace Vendor\Module\Controller\Adminhtml\Config;

class Index extends \Magento\Backend\App\Action {

    protected $resultLayoutFactory;

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

    /**
     * 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;
    }
}

Now working for you, Let me know if you have any issue with.

0

Like Raphael's answer, the acl file seems missing.

Also, the menu does not need the second add node and this could break the first node that seems good enough to me.

the _isAllowed function is mentioned in Raphael's post but I can't see any update in your post.

I have added a module in a public repo that shows a backend screen. Please take a look at backend screen

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.