我一直在尝试在 Magento2 中设置一个基本模块,尽管做了所有理想的更改,但它仍然抛出 404。下面是与该模块相关的代码。我的供应商名称是Chirag,模块名称是HelloWorld。
\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml
\n\n<?xml version="1.0"?>\n<!--\n/**\n * Copyright \xc2\xa9 2015 Magento. All rights reserved.\n * See COPYING.txt for license details.\n */\n-->\n<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">\n <module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">\n </module>\n</config>\n
Run Code Online (Sandbox Code Playgroud)\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml
\n\n<?xml version="1.0"?>\n<!--\n/**\n * Copyright \xc2\xa9 2015 Magento. All rights reserved.\n * See COPYING.txt for license details.\n */\n-->\n<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/App/etc/routes.xsd">\n <router id="standard">\n <route id="helloworld" frontName="helloworld">\n <module name="Chirag_HelloWorld" />\n </route>\n </router>\n</config>\n
Run Code Online (Sandbox Code Playgroud)\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
\n\n<?php\n/**\n *\n * Copyright \xc2\xa9 2015 Magento. All rights reserved.\n * See COPYING.txt for license details.\n */\nnamespace Chirag\\HelloWorld\\Controller\\Index;\n\nclass Index extends \\Magento\\Framework\\App\\Action\\Action\n{\n /**\n * \n *\n * @return void\n */\n public function execute()\n {\n protected $resultPageFactory;\n public function __construct(\n \\Magento\\Framework\\App\\Action\\Context $context,\n \\Magento\\Framework\\View\\Result\\PageFactory $resultPageFactory\n )\n {\n parent::__construct($context);\n $this->resultPageFactory = $resultPageFactory;\n }\n\n public function execute()\n {\n return $this->resultPageFactory->create();\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php
\n\n<?php\nnamespace Chirag\\HelloWorld\\Block;\n\nclass HelloWorld extends \\Magento\\Framework\\View\\Element\\Template\n{\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
\n\n<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">\n <body>\n <referenceContainer name="content">\n <block class="Chirag\\HelloWorld\\Block\\HelloWorld" name="helloworld" template="helloworld.phtml" />\n </referenceContainer>\n </body>\n</page>\n
Run Code Online (Sandbox Code Playgroud)\n\n/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
\n\n<h1> test hello to Magento 2 !! </h1>\n
Run Code Online (Sandbox Code Playgroud)\n\n任何形式的帮助将非常感激。
\n小智 5
首先尝试重命名route.xml
为routes.xml
,看看是否可以解决问题。
接下来尝试更改控制器中的代码,尝试更改公司/模块名称:
<?php
namespace YourCompany\ModuleName\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
protected $resultPageFactory;
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
return $this->resultPageFactory->create();
}
}
Run Code Online (Sandbox Code Playgroud)
另外,helloworld_index_index.xml
您可以尝试将模板装饰更改为:
template="YourCompany_ModuleName::templatename.phtml"
Run Code Online (Sandbox Code Playgroud)
最后你可以尝试将module.xml
setup_version
声明更改为:
setup_version="2.0.0"
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助!