0

I am trying to print hello world using block and phtml file. here is my code, app/code/Mageplaza/HelloWorld/Block/Display.php

<?php
namespace Mageplaza\HelloWorld\Block;
class Display extends \Magento\Framework\View\Element\Template
{
  public function __construct(\Magento\Framework\View\Element\Template\Context $context)
  {
      parent::__construct($context);
  }

  public function sayHello()
  {
      return __('Hello World');
  }
}

app/code/Mageplaza/HelloWorld/Controller/Index/Display.php

<?php
namespace Mageplaza\HelloWorld\Controller\Controller\Index;

class Display extends \Magento\Framework\App\Action\Action{
protected $_pageFactory;
public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $pageFactory
)
{
    $this->_pageFactory = $pageFactory;
    return parent::__construct($context);
}
public function execute()
{
    return $this->_pageFactory->create();
}
}

view/frontend/layout/helloworld_index_display.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceContainer name="content">
    <block class="Mageplaza\HelloWorld\Block\Display" name="helloworld_display" template="Mageplaza_HelloWorld::sayhello.phtml" />
</referenceContainer>
</page>

view/frontend/templates/sayHello.phtml

<?php

 /**
 * @var \Mageplaza\HelloWorld\Block\Display $block
 */

echo $block->sayHello();

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 frontName="helloworld" id="helloworld">
        <module name="Mageplaza_HelloWorld"/>
    </route>
</router>
</config>

migrated from magento.meta.stackexchange.com Sep 5 '18 at 7:09

This question came from our discussion, support, and feature requests site for users of the Magento e-Commerce platform.

  • Please share routes.xml code? – Amit Bera Sep 5 '18 at 7:11
3

There is a mistake in your controller class namespace.

Replace

namespace Mageplaza\HelloWorld\Controller\Controller\Index;

with

namespace Mageplaza\HelloWorld\Controller\Index;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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