1
votes

I'm using Magento 2 version beta Merchant 1.0.0

I'm trying to create a new custom module. The custom module works but is showing a blank page.

How can I made my module use the main template?

That is what I have so far:

Folder Structure: Magento2

  -app
    -code
      -Vendor
        -Block
          --Hellow.php
        -Controller
          -Index
            --Index.php
        -etc
          --module.xml
          -frontend
            -routes.xml
        -view
          -frontend
            -layout
              --hellow_index_index.xml
            -templates
              --hellow.phtml
        --composer.json

Files content: 1) Composer.json:

{
"name": "vendor-software/hellow-sample-module",
"description": "module based on composer!",
"require": {
    "magento/magento-composer-installer": "*",
    "magento/product-community-edition": "2.0.0"
    },
"type": "magento2-module",
"version": "0.1.0",
"extra": {
    "map": [
        [
            "*",
            "Vendor/Hellow"
        ]
    ]
},
"authors": [
    {
        "name": "Vendor",
        "homepage": "https://www.vendor.com/",
        "role": "Developer"
    }
]
}

2) /Vendor/Hellow/Block/Hellow.php:

<?php
namespace Vendor\hellow\Block;

use Magento\Framework\View\Element\Template;

class Hellow extends Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }   

    public function __construct(Template\Context $context, array $data = [])
    {
        parent::__construct($context, $data);
        $this->_isScopePrivate = true;
    }
}

3) /Vendor/Hellow/Controller/Index/Index.php:

use Magento\Framework\App\Action\Action;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\Page;

class Index extends Action
{
    protected $resultPageFactory;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory
    )
    {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

4) /Vendor/Hellow/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="Vendor_Hellow" setup_version="2.0.0">
        </module>
    </config>

5) /Vendor/Hellow/etc/frontend/routes.xml:

<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="hellow" frontName="hellow">
                <module name="Vendor_Hellow" />
            </route>
        </router>
    </config>

6) /Vendor/Hellow/view/frontend/layout/hellow_index_index.xml:

<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 World</title>
        </head>
        <body>
            <referenceContainer name="content">
                <block class="Vendor\Hellow\Block\Hellow" name="hellow" template="hellow.phtml" />
            </referenceContainer>
        </body>
    </page>

7) /Vendor/Hellow/view/frontend/templates/hellow.phtml:

<h1>Hello World!</h1>

The result when I access the address: localhost/magento2/hellow/index/ is a blank page. But it should show a blank page inside the Magento Main Page strucutre, showing the header, menu, content and so one.

What I doing wrong?

Thanks

3

3 Answers

0
votes

Simply add layout="2columns-left" in your page node of layout xml. This will resolve your blank page problem.

1
votes

It's look like you have a mistake in a name of layout handle. It should be hellow_index_index.xml and you have hellow_index.index.xml. Also check if you didn't miss namespace in Vendor/Hellow/Controller/Index/Index.php.

0
votes

You can refer to the git I have shared in order to develop a Hello World module.Hello World Module. Let me know if you find any difficulties.