Search code examples
magentomagento2

Custom module not working in Magento2


I have been trying to setup a basic module in Magento2, it keeps throwing 404 despite doing all the ideal changes. Below is the code related to the module. My vendor name is Chirag and module name is HelloWorld.

/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">
    </module>
</config>

/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="Chirag_HelloWorld" />
        </route>
    </router>
</config>

/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Chirag\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * 
     *
     * @return void
     */
    public function execute()
    {
        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();
        }
    }
}

/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php

<?php
namespace Chirag\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{

}

/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_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="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
        </referenceContainer>
    </body>
</page>

/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml

<h1> test hello to Magento 2 !! </h1>

Any kind of help would be really appreciated.


Solution

  • Below changes led to correct answer for me :

    Index controller - magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php

    <?php
    /**
     *
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Chirag\HelloWorld\Controller\Index;
    
    class Index extends \Magento\Framework\App\Action\Action
    {
        /**
         * Show Contact Us page
         *
         * @return void
         */
        public function execute()
        {
            $this->_view->loadLayout();
            $this->_view->getLayout()->getBlock('helloworld');
            $this->_view->renderLayout();
        }
    }
    

    Block - magento2/app/code/Chirag/HelloWorld/Block/Question.php

    <?php
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    namespace Chirag\HelloWorld\Block;
    
    use Magento\Framework\View\Element\Template;
    
    /**
     * Main contact form block
     */
    class Question extends Template
    {
        /**
         * @param Template\Context $context
         * @param array $data
         */
        public function __construct(Template\Context $context, array $data = [])
        {
            parent::__construct($context, $data);
            $this->_isScopePrivate = true;
        }
    }
    

    Layout file - magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml

    <?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2015 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <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="Chirag\HelloWorld\Block\Question" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml">
                </block>
            </referenceContainer>
        </body>
    </page>
    

    View template - magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml

    <?php echo 'helloworld view'; ?>
    <h1> Hello World </h1>