Magento – First module HelloWorld in Magento 2.0 doesn’t work

command lineerrormagento2module

  • CentOS Linux release 7.1.1503 (Core)
  • WHM version 11.52.1.3 (Root access)
  • Cpanel Apache 2.4.16
  • PHP 5.5.30
  • MySQL 5.6
  • Magento-CE-2.0.0+Samples.* Extracted (.tar.bz2) on my server ,and ran
    the Setup Wizard.

I did this example
http://cedcommerce.com/blog/magento2/hello-world-module/


public_html/app/code/Ced/HelloWorld/etc/module.xml

<config>     
      <module name="Ced_HelloWorld" schema_version="2.0.0" setup_version="0.0.1">
      </module>
</config>

I also try

<?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="Ced_HelloWorld" setup_version="1.0.0">
    </module>
</config>

public_html/app/code/Ced/HelloWorld/etc/frontend/routes.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi: ="../../../../../ ../lib/internal/
Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Ced_HelloWorld" />
        </route>
    </router>
</config>

I also try

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi: ="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Ced_HelloWorld" />
        </route>
    </router>
</config>

I also add

public_html/app/etc/config.php

 ...
      'Magento_Wishlist' => 1,
        'Magento_WishlistSampleData' => 1,
        'Ced_HelloWorld' => 1,
      ),

I also run this commands line

php-cli bin/magento cache:clean

php-cli -f bin/magento module:enable –clear-static-content
Ced_HelloWorld

->Unknown module(s): 'Ced_HelloWorld'

php-cli bin/magento setup:upgrade

php-cli bin/magento setup:di:compile

I also flush the cache of the admin panel.


I checked and pdo_mysql extension is installed
Permission directories 755 files and 644 for all Magento files


In browser magentodir/helloworld

Whoops, our bad…
The page you requested was not found, and we have a fine guess why.
If you typed the URL directly, please make sure the spelling is correct.
If you clicked on a link to get here, the link is outdated.

Best Answer

Follow bellow steps

Step : 1 (module.xml) app/code/Ccc/HelloWorld/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="Ccc_HelloWorld" setup_version="1.0.1">
    </module>
</config>

Step : 2 (routes.xml) app/code/Ccc/HelloWorld/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="helloworld" frontName="helloworld">
        <module name="Ccc_HelloWorld" />
    </route>
</router>
</config>

Step : 3 (registration.php) app/code/Ccc/HelloWorld/registration.php

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

Step : 4 (Index.php) app/code/Ccc/HelloWorld/Controller/Index/Index.php

<?php 
namespace Ccc\HelloWorld\Controller\Index;

use Magento\Framework\View\Result\PageFactory;

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

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Hello World'));
        return $resultPage;        
    }
}

Step : 5 (HelloWorld.php) app/code/Ccc/HelloWorld/Block/HelloWorld.php

<?php
namespace Ccc\HelloWorld\Block;

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

Step : 6 (helloworld_index_index.xml) app/code/Ccc/HelloWorld/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Ccc\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

Step : 7 (helloworld.phtml) app/code/Ccc/HelloWorld/view/frontend/templates/helloworld.phtml

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

Step : 8 (rerun magento setup - this will re-generate app/etc/config.php & enable the module)

php-cli bin/magento setup:upgrade

I also flush the cache of the admin panel.