2
votes

I have created a simple custom module in Magento 2. But its not working. Could anyone please suggest me where I went wrong?

My Code is

app/etc/config.xml

'Sparx_Helloworld' => 1,

app/code/Sparx/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="Sparx_Helloworld" schema_version="0.0.1" active="true">
    </module>
</config>

app/code/Sparx/Helloworld/Controller/Index/Index.php

<?php
namespace Sparx\Helloworld\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/Sparx/Helloworld/Block/Helloworld.php

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

app/code/Sparx/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="Sparx_Helloworld" />
        </route>
    </router>
</config>

app/code/Sparx/Helloworld/etc/view/frontend/layout/helloworld_index_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Welcome to Magento World</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block name="helloworld" template="helloworld.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

app/code/Sparx/Helloworld/view/frontend/templates/helloworld.phtml

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

I getting below error enter image description here

I'm not sure what is wrong. Please do the needful.

Thanks

1

1 Answers

3
votes

your module.xml should look like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Sparx_Helloworld" setup_version="0.0.1">
    </module>
</config>

if you are working on the latest version of magento 2 (1.0.0-beta5) you will need this file also in the Sparx/Helloworld

registration.php

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

and you might need to run php bin/magento setup:upgrade in the command line instead of adding 'Sparx_Helloworld' => 1, in the config.php file