7

I'm using Magento 2 version beta-1.0.0 and trying to create a simple new custom module. The custom module works but is showing a blank page on body content.

The module done in below way.

Folder Structure: Hello

-app
    -code
        -Magento
            -Hello
                -Block
                    --Hello.php
                -Controller
                    -Index
                        --Index.php
                -etc
                    --module.xml
                    -frontend
                        --routes.xml
                -view
                    -frontend
                        -layout
                            --hello_index_index.xml
                        -templates
                            --hello.phtml

Files are as follow.

(1) app/code/Magento/Hello/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Magento_Hello" setup_version="0.0.1"/>
</config>

(2) app/code/Magento/Hello/etc/frontend/routes.xml

<?xml version="1.0" ?>
<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="hello" frontName="hello">
            <module name="Magento_Hello"/>
        </route>
    </router>
</config>

(3) app/code/Magento/Hello/Controller/Index/Index.php

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();
    }
}

(4) app/code/Magento/Hello/Block/Hello.php

<?php
namespace Magento\Hello\Block;

class Hello extends \Magento\Framework\View\Element\Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

(5) app/code/Magento/Hello/view/frontend/layout/hello_index_index.xml

<?xml version="1.0" ?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
    <head>
        <title>Hello to Magento 2.0</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block class="Magento\Hello\Block\Hello" name="hello" template="hello.phtml" />
        </referenceContainer>
    </body>
</page>

(6) app/code/Magento/Hello/view/frontend/templates/hello.phtml

<?php echo "This is simple hello module on Magento 2.0"; ?>

Once module created I've updated Magento setup as well.

cd [magento2_root_folder_path]
php bin/magento setup:upgrade

And if I go to

http://localhost/magento2/hello/index/index it shows blank under "body" tag. I'm sure module is loading properly since "head" tag appears with the Title.

Any help would be highly appreciated.

6
  • Don't see anything wrong at a glance. Blank after a point suggests PHP error. Have you checked your server's PHP error logs? Or tried enabling error displaying?
    – Ryan Hoerr
    Sep 27, 2015 at 14:32
  • @Ryan: Yes, I've checked PHP error logs and nothing is appeared there. Enabled error displaying by adding error_reporting(E_ALL) & ini_set('display_errors', '1') on index.php. But didn't appear any errors. Module seems working since <title> tag appears correctly, but nothing under <body> tag.
    – Oshan
    Sep 27, 2015 at 15:14
  • Is that the correct folder structure because it seems you are missing a level between Magento and your module structure. Probably just a typo but should be contained in 'Hello'.
    – Smartie
    Sep 28, 2015 at 9:20
  • @Smartie: My bad. yes, it was a typo error. I've updated the question.
    – Oshan
    Sep 28, 2015 at 10:41
  • 1
    try adding this layout="2columns-left" on the page node in the hello_index_index.xml file
    – Marius
    Sep 28, 2015 at 10:52

1 Answer 1

17

You need to specify the layout for the page.
So add this layout="2columns-left" on the <page> node in hello_index_index.xml.
You can also use 1column, 2columns-right, 3columns or empty.

2
  • Is there error logging for this Marius? If not I may open an issue on git. Jun 19, 2017 at 8:18
  • I have no idea.
    – Marius
    Jun 19, 2017 at 8:25

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