Chiragit007
Chiragit007

Reputation: 1646

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.

Upvotes: 1

Views: 6436

Answers (5)

waqas saeed
waqas saeed

Reputation: 1

First rename route.xml to routes.xml

Modify controller Class

<?php
namespace Chirag\HelloWorld\Controller\Index;;
class Index extends \Magento\Backend\App\Action
{
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }
    public function execute()
    {
    //   print_r($this->reportFactory->create()->getCollection());
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->prepend(__('Form Grid'));
        return $resultPage;
    }
}

after this run the command
    php/bin/magento c:f
    php/bin/magento setup:upgrade

Upvotes: 0

Anuradha F
Anuradha F

Reputation: 36

First of all, rename route.xml to routes.xml

app/code/Chirag/HelloWorld/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="helloworld" frontName="helloworld">
            <module name="Chirag_HelloWorld" />
        </route>
    </router>
</config>

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

You can't use __construct inside the execute method and these two should be separated.

<?php
/**
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Chirag\HelloWorld\Controller\Index;

use Magento\Framework\App\Action\Action;

class Index extends Action
{
    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

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

    /**
     * @return mixed
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

You should use the below format when you are calling the template in layout xml.

eg: Chirag_HelloWorld::helloworld.phtml

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="Chirag_HelloWorld::helloworld.phtml" />
        </referenceContainer>
    </body>
</page>

app/code/Chirag/HelloWorld/Block/HelloWorld.php

<?php
namespace Chirag\HelloWorld\Block;

class HelloWorld extends \Magento\Framework\View\Element\Template
{
    public function getHelloWorldText()
    {
        return __('Hello World');
    }
}

app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml

<?= /* @noEscape */ $block->getHelloWorldText() ?>

Once you done run the below commands to apply the changes.

bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento c:f

Use -f as a parameter if you are in developer mode.

Use bin/magento deploy:mode:show to check the mode.

Happy Coding!!

Upvotes: 0

bachlee89
bachlee89

Reputation: 757

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

Upvotes: 0

Greg Crane
Greg Crane

Reputation: 91

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!

Upvotes: 5

Chiragit007
Chiragit007

Reputation: 1646

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>

Upvotes: 1

Related Questions