Magento 2 Custom Module – Troubleshooting Non-Working Modules

magento2php-5.4

I am created custom module in magento 2, but it's not working. Could you please suggest me where i went wrong?

my Code is:

app/etc/config.php

'modules' => 
  array (
    'Magento_Hello' => 1,
         ),

app/code/local/Magento/Hello/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="Magento_Hello" schema_version="2.0.0">
        <sequence>
            <module name="Magento_Hello"/>
        </sequence>
    </module>
</config>

app/code/local/Magento/Hello/etc/frontend/routes.xml

<?xml version="1.0"?>
<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>

app/code/local/Magento/Hello/Controller/Index/Index.php

<?php
namespace Magento\Hello\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

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

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

app/code/local/Magento/etc/frontend/layout/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">
   <head>
       <title>Hello World</title>
   </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Hello\Block\Hello" name="hello" template="success.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

app/code/local/Magento/etc/frontend/templates/success.phtml

<?php echo 'Hi I am Magento 2'; ?>

Where I went wrong?

Thanks in advance.

Best Answer

Here are the issues with your module.
First of all lose the local in the folder path. There are no more codepools in magento 2.
So your module.xml should be placed in app/code/Magento/Hello/etc/module.xml. Do the same for the rest of the files.
And by the way. Use an other vendor name. Leave Magento for the core team. It works if you use the same one, but it's not a best practice.
Second...lose the <sequence> tag from module.xml. The way you did it, you tell Magento that your module needs to be loaded before itself which can't be done. The <sequence> tag is used to force load order and helps Magento deal with circular dependencies that haven't been removed yet.

Third...the layout file should not be placed in etc. It should be in app/code/Magento/Hello/view/frontend/layout/hello_index_index.xml.

The same goes for the template file. It should be in app/code/Magento/Hello/view/frontend/templates/success.phtml

And as a tip. In the controller you should not use $this->_view. It is deprecated and it will be gone soon. Instead make your controller look like this:

<?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();
    }
}

And clear the contents of folder var/generation