自定义模块中的空白页(Magento 2 Beta Merchant Version 1.0.0)

Blank page in a custom module (Magento 2 Beta Merchant Version 1.0.0)

我正在使用 Magento 2 版本 beta Merchant 1.0.0

我正在尝试创建一个新的自定义模块。自定义模块有效,但显示空白页。

如何让我的模块使用主模板?

这就是我目前的情况:

文件夹结构: 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

文件内容: 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>

访问地址localhost/magento2/hellow/index/的结果是空白页。但它应该在 Magento 主页结构中显示一个空白页面,显示 header、菜单、内容等。

我做错了什么?

谢谢

您的布局句柄名称似乎有误。它应该是 hellow_index_index.xml 而你有 hellow_index.index.xml。还要检查你是否没有错过 Vendor/Hellow/Controller/Index/Index.php.

中的命名空间

大家可以参考git我分享的开发Hello World模块Hello World Module。如果您发现任何困难,请告诉我。

只需在布局 xml 的页面节点中添加 layout="2columns-left"。这将解决您的空白页问题。