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.
-
I am wondering how you get answer. have you check your registration.php is correct? your registration.php should like @Rajkumar .E answer.Bilal Usean– Bilal Usean2016年06月02日 08:27:50 +00:00Commented Jun 2, 2016 at 8:27
-
@BilalUsean I fixed it and now the module is working properly.Sunil Neurgaonkar– Sunil Neurgaonkar2016年06月02日 09:03:59 +00:00Commented Jun 2, 2016 at 9:03
4 Answers 4
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.
-
tun php bin/magento setup:upgrade then i run php bin/magento setup:di:compile and now i have errors after running that commandSunil Neurgaonkar– Sunil Neurgaonkar2016年06月01日 09:00:24 +00:00Commented Jun 1, 2016 at 9:00
-
which error are generate plz show thatRakesh Jesadiya– Rakesh Jesadiya2016年06月01日 09:05:51 +00:00Commented 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-tenantntzz– ntzz2016年06月01日 09:09:57 +00:00Commented 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.Sunil Neurgaonkar– Sunil Neurgaonkar2016年06月01日 09:18:54 +00:00Commented Jun 1, 2016 at 9:18
-
have you remove var folder from rootRakesh Jesadiya– Rakesh Jesadiya2016年06月01日 09:19:56 +00:00Commented Jun 1, 2016 at 9:19
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 :
- Unzip the downloaded module and paste into the "your_project/app/code/your downloded module
- sudo php bin/magento setup:upgrade
- rm -rf var/di/* var/generation/* var/cache/* var/page_cache/* var/view_preprocessed/* var/composer_home/cache/*
- sudo php bin/magento setup:di:compile
- sudo php bin /magento cache:clean
- sudo chmod 777 var -R
- sudo chmod 777 pub -R
After completing these steps you will find your Magento module working.
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.
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?