[英]Magento 2 : Custom Module not working
我在 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();
}
}
app/code/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>
app/code/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>
app/code/Sparx/Helloworld/view/frontend/templates/helloworld.phtml
<?php echo 'Successful! This is a simple helloworld module in Magento 2'; ?>
我不确定出了什么问题。 请只做那些需要的。
谢谢
您的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
,而不是在config.php
文件中添加'Sparx_Helloworld' => 1,
首先,请确保您已禁用Magento缓存并已打开开发人员模式。 开发人员模式将显示所有错误。
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="1.0.0">
</module>
</config>
要注册该模块,您必须具有registration.php
,该文件应如下所示:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Sparx_Helloworld',
__DIR__
);
您还需要其他文件。
有关Magento 2模块创建的完整教程,请访问: 如何创建Magento 2模块
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.