我在 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'; ?>
我不确定出了什么问题。请只做那些需要的。
谢谢