1
votes

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.

3
hello @Chiragit007, Is it possible to use this module for maintaining REST API call ? - SagarPPanchal

3 Answers

4
votes

First try to rename route.xml to routes.xml and see if that fixes the issue.

Next try changing your code in the controller, try change company/module names:

<?php

namespace YourCompany\ModuleName\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();
    }
}

also in your helloworld_index_index.xml you can try changing the template decoration to:

template="YourCompany_ModuleName::templatename.phtml"

lastly you can try changing the module.xml setup_version declaration to:

setup_version="2.0.0"

I hope this helps!

1
votes

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>
0
votes

You need to follow the below steps to resolve:

  1. You have to change route.xml to routes.xml

  2. Run setup upgrade command:

    php bin/magento s:up

  3. Go to your_base_link/helloworld/index/index

  4. Enjoin your result