2

我在 Magento 2 中创建了一个简单的自定义模块。但它不起作用。谁能建议我哪里出错了?

我的代码是

应用程序/etc/config.xml

'Sparx_Helloworld' => 1,

应用程序/代码/Sparx/Helloworld/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="Sparx_Helloworld" schema_version="0.0.1" active="true">
    </module>
</config>

应用程序/代码/Sparx/Helloworld/Controller/Index/Index.php

<?php
namespace Sparx\Helloworld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->_view->loadLayout();
        $this->_view->getLayout()->initMessages();
        $this->_view->renderLayout();
    }
}

应用程序/代码/Sparx/Helloworld/Block/Helloworld.php

<?php
namespace Sparx\Helloworld\Block;
class Helloworld extends \Magento\Framework\View\Element\Template
{
    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
}

应用程序/代码/Sparx/Helloworld/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="helloworld" frontName="helloworld">
            <module name="Sparx_Helloworld" />
        </route>
    </router>
</config>

应用程序/代码/Sparx/Helloworld/etc/view/frontend/layout/helloworld_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>Welcome to Magento World</title>
    </head>
    <body>
        <referenceContainer name="content">
            <block name="helloworld" template="helloworld.phtml">
            </block>
        </referenceContainer>
    </body>
</page>

应用程序/代码/Sparx/Helloworld/view/frontend/templates/helloworld.phtml

<?php echo 'Successful! This is a simple helloworld module in Magento 2'; ?>

我得到以下错误 在此处输入图像描述

我不确定出了什么问题。请只做那些需要的。

谢谢

4

1 回答 1

3

您的 module.xml 应如下所示:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Sparx_Helloworld" setup_version="0.0.1">
    </module>
</config>

如果您正在使用最新版本的 magento 2 (1.0.0-beta5),您还需要在Sparx/Helloworld

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Sparx_Helloworld',
    __DIR__
);

并且您可能需要php bin/magento setup:upgrade在命令行中运行而不是'Sparx_Helloworld' => 1,config.php文件中添加

于 2015-10-30T14:03:10.587 回答