0

I am working on a simple module and trying to display a block. All that is showing up is a blank page. If I put an echo "blah"; exit() in my controller, it shows the output. No errors in my log files. Below is the code

registration.php

<?php

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

Magento2/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="magento2" frontName="helloworld">
                        <module name="Magento2_HelloWorld" />
                </route>
        </router>
</config>

Magento2/HelloWorld/Block/Index.php

<?php

namespace Magento2\HelloWorld\Block;


class Index extends \Magento\Framework\View\Element\Template {

    public function __construct(\Magento\Catalog\Block\Product\Context $context, array $data = []) {

        parent::__construct($context, $data);

    }


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

    protected function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

}

Magento2/HelloWorld/Controller/Index/Index.php

<?php

namespace Magento2\HelloWorld\Controller\Index;

use \Magento\Framework\App\Action\Action;


class Index extends Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

Magento2/HelloWorld/view/frontend/layout/helloworld_index_index

<?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="Magento2\HelloWorld\Block\Index" name="HelloWorld_index" template="Magento2_HelloWorld::helloworld.phtml" />
    </referenceContainer>

</page>

Magento2/HelloWorld/view/frontend/templates/helloworld.phtml

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

After running http://localhost/mage2/helloworld/index/index URL, I can see controller being executed but the block is not being called.

Module Output

  • Let me know if you have any issue. – Prince Patel Oct 7 '17 at 14:18
0

Everything is ok in your code. You just need to change this file

Magento2/HelloWorld/etc/frontend/routes.xml

router id and frontName must be same

<?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="Magento2_HelloWorld" />
        </route>
    </router>
</config>

Now remove var/generation folder and flush cache

  • I have answered this question before your answer...You have copied my answer i think – Ranganathan Oct 14 '17 at 10:18
  • @Ranganathan.S. I think you are new in StackExchange. I have copied the code from the question same as you did. This does not mean that I copied your answer :) – Prince Patel Oct 14 '17 at 11:01
  • I have posted exact answer already...why you post same answer again... – Ranganathan Oct 14 '17 at 11:16
  • @Ranganathan.S StackExchange is not only for one user. Everyone can post an answer here. If you think that my answer is a copy of your answer then you can flag it without more explanation and unwanted commenting here. Comment is used for only solve questions issue, not for this type of unwanted comment. – Prince Patel Oct 14 '17 at 11:56
  • Good Smalification Bro – Ranganathan Oct 14 '17 at 12:04
0

I have updated your routes.xml
change your id to helloworld

<?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="Magento2_HelloWorld" />
                </route>
        </router>
</config>

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.