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

时间:2015-09-02 14:59:26

标签: magento2

我正在使用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主页结构中显示一个空白页面,显示标题,菜单,内容等。

我做错了什么?

由于

2 个答案:

答案 0 :(得分:0)

看起来你的布局句柄名称有错误。它应该是hellow_index_index.xml,你有hellow_index.index.xml。还要检查Vendor / Hellow / Controller / Index / Index.php中是否没有错过命名空间。

答案 1 :(得分:0)

只需在layout xml的页面节点中添加 layout =&#34; 2columns-left&#34; 即可。这将解决您的空白页问题。