Magento 2.0 Module Not Working – Troubleshooting Guide

magento2module

Here is how my module structure looks

Hello.php

app/code/Magento/Hello/Block/Hello.php

namespace Magento\Hello\Block;
class Hello extends \Magento\Framework\View\Element\Template
{
public function _prepareLayout()
{
    return parent::_prepareLayout();
}
}

Index.php

namespace Magento\Hello\Controller\Index;

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

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

routes.xml

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

module.xml

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

hello_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
    <referenceContainer name="content">
        <block class="Magento\Hello\Block\Hello" name="hello" template="Magento_Hello::success.phtml">
        </block>
    </referenceContainer>
</body>
</page>

success.phtml

<?php echo ‘Successful! This is a simple module in Magento 2.0′; ?>

registration.php

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

I have added the 'Magento_Hello' => 1 to the etc/config.php.

I am running Magento 2 on localhost.

Best Answer

You have to run command

php bin/magento setup:upgrade

using cmd.

This command enable module in your system and one entry are generated under setup_module table.

Directly you can't generate entry inside app/etc/config.php file.

Remove var folder and try again.