1

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.

2
  • I am wondering how you get answer. have you check your registration.php is correct? your registration.php should like @Rajkumar .E answer. Jun 2, 2016 at 8:27
  • @BilalUsean I fixed it and now the module is working properly. Jun 2, 2016 at 9:03

4 Answers 4

0

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.

12
  • tun php bin/magento setup:upgrade then i run php bin/magento setup:di:compile and now i have errors after running that command Jun 1, 2016 at 9:00
  • which error are generate plz show that Jun 1, 2016 at 9:05
  • Which version you've got of Magento 2? In the last update they fix the setup:di:compile , if you have previous version, try to use setup:di:compile:multi-tenant
    – ntzz
    Jun 1, 2016 at 9:09
  • I had the php memory error but i fixed it. so the command is run successfully. everything is successfully done but still i cant see the module. Jun 1, 2016 at 9:18
  • have you remove var folder from root Jun 1, 2016 at 9:19
1

The simple solution is to create module online using "Magento 2 Module Creator | CedCommerce" . you need to complete 2-3 basic and simple steps then you can download and install your plugin to your website by following steps :

  1. Unzip the downloaded module and paste into the "your_project/app/code/your downloded module
  2. sudo php bin/magento setup:upgrade
  3. rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/* var/composer_home/cache/*
  4. sudo php bin/magento setup:di:compile
  5. sudo php bin /magento cache:clean
  6. sudo chmod 777 var -R
  7. sudo chmod 777 pub -R

After completing these steps you will find your Magento module working.

0

I thinks you need to change registration.php like this

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

Then Delete var folder and run setup:upgrade command. Then check it.

0
template="Magento_Hello::success.phtml"

Is that a valid way to define a template? In your hello_index_index.xml? I thought that was for block methods?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.