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>
1
  • Please share routes.xml code?
    – Amit Bera
    Sep 5, 2018 at 7:11

1 Answer 1

3

There is a mistake in your controller class namespace.

Replace

namespace Mageplaza\HelloWorld\Controller\Controller\Index;

with

namespace Mageplaza\HelloWorld\Controller\Index;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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