Custom module not working in Magento2

Custom module not working in Magento2

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Custom module not working in Magento2

Why Your Magento2 Custom Module Isn’t Working: A Comprehensive Guide

You’ve set up a basic module in Magento2 and you’re facing a frustrating 404 error despite making all the ideal changes. Let’s dive into your code and find out what’s going wrong.

Checking the Basic Configuration of the Module

The module.xml File

Your module.xml file seems to be correctly configured:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1"/>
</config>

The name attribute is set to “Chirag_HelloWorld”, which matches your vendor and module names. The schema_version and setup_version attributes are also properly specified.

Analyzing the Routing Configuration

The route.xml File

Your routing configuration looks good as well:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Chirag_HelloWorld" />
        </route>
    </router>
</config>

You’ve defined a new route “helloworld” with the frontName set to “helloworld”, and the module is correctly mapped to “Chirag_HelloWorld”.

Examining Your Controller

The Index.php File in the Controller Directory

The controller looks mostly correct:

namespace Chirag\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    //...
}

You’ve extended the base controller class correctly. However, there are a few issues with your constructor and execute method.

The Constructor Issue

In your constructor:

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

You’ve correctly injected the necessary dependencies, but you’re using $this->resultPageFactory without a namespace. In PHP, variables starting with $this should have their full namespace specified to avoid conflicts.

The Execute Method Issue

In your execute method:

public function execute()
{
    return $this->resultPageFactory->create();
}

You’re returning the result page directly, but you haven’t added any content to it. By default, this will just display an empty white screen.

Fixing Your Controller Issues

To fix these issues, update your controller like this:

namespace Chirag\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

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

    public function execute()
    {
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();

        // Add your content here, for example:
        $block = $resultPage->getLayout()->addBlock(\Chirag\HelloWorld\Block\HelloWorld::class);
        $block->setTemplate('Chirag_HelloWorld::helloworld.phtml');

        return $resultPage;
    }
}

Conclusion

In conclusion, you had a few key issues with your controller that were preventing the module from working correctly. By updating the constructor to use the full namespace for $this variables and adding content to your execute method, you should be able to fix these 404 errors.

Remember, when dealing with Magento2 modules, careful attention to detail is crucial. Even small syntax or composition issues can cause unpredictable behavior.

For more detailed information on developing custom modules in Magento2, check out the official documentation or visit the Magento Dev Docs.

This comprehensive guide should help you get your Magento2 custom module working as expected. If you’re still facing issues, feel free to reach out for further assistance!

Magento 2 Extensions Configuration Errors

Our network