5

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.

asked Feb 5, 2015 at 13:15
7
  • 2
    What do you mean by "doesn't work"? Commented Feb 5, 2015 at 13:16
  • 1
    @Marius, Yes, not working. Commented Feb 5, 2015 at 13:17
  • Do you get an error? the module is not picked up? something crashes? be more exact Commented Feb 5, 2015 at 13:17
  • @Marius, I am getting 404 Not Found Error. Commented Feb 5, 2015 at 13:18
  • @Marius, Is there any options to Enable Logs in Magento 2? (From Magento Developer Section Enable Logs = Yes) like this. Commented Feb 5, 2015 at 13:22

1 Answer 1

16

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

answered Feb 5, 2015 at 13:40
4
  • Thanks you now working fine. Yes your correct, after I removing <sequence> it's working. <sequence> (from Magento 2) means <depends> (from Magento) tag?. Commented Feb 5, 2015 at 13:56
  • Yes...Kind of.. Commented Feb 5, 2015 at 13:58
  • 3
    More information on sequence can be found here: devdocs.magento.com/guides/v1.0/architecture/modules/… Commented Feb 5, 2015 at 14:56
  • 1
    Deleting the contents of folder var/generation is important else you will get Recoverable Error: Argument 2 passed to... Commented Oct 21, 2015 at 20:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.