Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top