I'm attempting to create my own module with a custom template however in my browser, I am unable to view the 'hello world' output.

Gregt/Hello/block - helloworld.php 

    <?php
    namespace GregT\Hello\Block;

    class Helloworld extends \Magento\Framework\View\Element\Template
    {
    public function getHelloWorldTxt()
    {
        return 'Hello world!';
       }
    }


Gregt/Hello/controller/index - index.php 

    <?php

    namespace GregT\Hello\Controller\Index;

    use Magento\Framework\App\Action\Context;

    class Index extends \Magento\Framework\App\Action\Action
    {
     protected $_resultPageFactory;

    public function __construct(Context $context, 
    \Magento\Framework\View\Result\PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    public function execute()
    {
        $resultPage = $this->_resultPageFactory->create();
        echo ("hello");
        return $resultPage;
       }
    }

Gregt/Hello/VIEW/frontend/layout - helloworld_index_index.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/
    Framewo rk/View/Layout/etc/page_configuration.xsd" layout="1column">
      <body>
        <referenceContainer name="content">
            <block class="GregT\Hello\Block\Helloworld" name="helloworld" 
    template="GregT_Hello::helloworld.phtml" />
        </referenceContainer>
    </body>
    </page>

Gregt/Hello/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="GregT_Hello" />
        </route>
    </router>
    </config>

Gregt/Hello/etc - module.xml 

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


Gregt/Hello/view/frontend/templates - helloworld.phtml 

    <h1><?php echo $block->getHelloWorldTxt(); ?></h1>

Gregt/Hello - registration.php 

    <?php

    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'GregT_Hello',
     __DIR__
    );

[![enter image description here][2]][2]

  [2]: https://i.stack.imgur.com/NWuFv.png
  • try with this <h1><?php echo $block->getHelloWorldTxt(); ?></h1> – kunj Aug 14 at 9:00
  • Your layout file is at wrong place I have added the answer with the correct place and content. – kunj Aug 14 at 9:30
  • Hi, Greg, welcome. You don't need to add "SOLVED" to the question, and also don't need to tag the title (the tagging system will do that for you). I've posted an edit removing those elements from the title. The stackexchange system will mark the question as solved (the green checkmark on accepted answer) and also show the main tag automatically. – Mindwin Aug 14 at 13:10
up vote 4 down vote accepted

Check below solution, May be helpful to you.

You have created Gregt/Hello/controller/index - index.php And Gregt/Hello/block - helloworld.php

Also layout file wrong, It should be Gregt/Hello/view/frontend/layout - helloworld_index_index.xml

folder name should be Controller not controller, Block not block. Please correct folder name and check.

  • Changed all these but still isn't showing – Greg Aug 14 at 9:06
  • Also you should apply changes as per @Amit bera – Suresh Chikani Aug 14 at 9:18

You did a small mistake

<?php echo $this->getHelloWorldTxt(); ?>

Should be

<?php echo $block->getHelloWorldTxt(); ?>

Also have to change

name="helloworld" 
template="helloworld.phtml" 

to

name="helloworld" 
template="GregT_Hello::helloworld.phtml" 
  1. Controller path should be like this:

    app/code/Gregt/Hello/Controller/Index/Index.php 
    
  2. in execute() try to put inside return "Hello";

  3. Call this path : domaine.com/helloworld and see If you see Hello

  • Hi, I'm now able to see Hello at the top of the page. – Greg Aug 14 at 8:55
  • Did you updated the files to Capitalize letters and refreshed th cache ? – Prince Aug 14 at 8:59
  • Yes but I don't think it is using the template and instead just echoing from index.php – Greg Aug 14 at 9:00
  • Yes I mean the controller files from app/code/Gregt/Hello/controller/index/index.php to app/code/Gregt/Hello/Controller/Index/Index.php – Prince Aug 14 at 9:02
  • 1
    yes I knew it, I told you in the previous comment (errors in your module including the controller and layout) but it was necessary to debug the controller before, the essential is that you have solved your problem, good luck :) – Prince Aug 14 at 9:38

Your lyout file should be like this:

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="GregT\Hello\Block\Helloworld" name="helloworld"
                   template="GregT_Hello::helloworld.phtml" />
        </referenceContainer>
    </body>
</page>

And Place must be at app/code/GregT/Hello/view/frontend/layout/helloworld_index_index.xml

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.