自定义模块在 Magento2 中不起作用
Custom module not working in Magento2
我一直在尝试在 Magento2 中设置一个基本模块,尽管进行了所有理想的更改,它仍然抛出 404。下面是与模块相关的代码。我的供应商名称是 Chirag,模块名称是 HelloWorld。
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">
</module>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="Chirag_HelloWorld" />
</route>
</router>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
*
*
* @return void
*/
public function execute()
{
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();
}
}
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_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">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
</referenceContainer>
</body>
</page>
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<h1> test hello to Magento 2 !! </h1>
如有任何帮助,我们将不胜感激。
首先尝试将 route.xml
重命名为 routes.xml
,看看是否能解决问题。
接下来尝试更改控制器中的代码,尝试更改 company/module 名称:
<?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();
}
}
同样在您的 helloworld_index_index.xml
中,您可以尝试将模板装饰更改为:
template="YourCompany_ModuleName::templatename.phtml"
最后,您可以尝试将 module.xml
setup_version
声明更改为:
setup_version="2.0.0"
希望对您有所帮助!
以下更改为我带来了正确答案:
索引控制器 - magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* Show Contact Us page
*
* @return void
*/
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->getBlock('helloworld');
$this->_view->renderLayout();
}
}
阻止 - magento2/app/code/Chirag/HelloWorld/Block/Question.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
/**
* Main contact form block
*/
class Question extends Template
{
/**
* @param Template\Context $context
* @param array $data
*/
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
}
布局文件 - magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\Question" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml">
</block>
</referenceContainer>
</body>
</page>
查看模板 - magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?php echo 'helloworld view'; ?>
<h1> Hello World </h1>
您需要按照以下步骤解决:
您必须将 route.xml
更改为 routes.xml
运行设置升级命令:
php bin/magento s:up
转到your_base_link/helloworld/index/index
输入你的结果
首先,将route.xml
重命名为routes.xml
app/code/Chirag/HelloWorld/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="Chirag_HelloWorld" />
</route>
</router>
</config>
索引控制器 - app/code/Chirag/HelloWorld/Controller/Index/Index.php
不能在execute
方法中使用__construct
,这两个应该分开。
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Action;
class Index extends Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* @return mixed
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
在布局中调用模板时应使用以下格式xml。
例如:Chirag_HelloWorld::helloworld.phtml
app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_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">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml" />
</referenceContainer>
</body>
</page>
app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldText()
{
return __('Hello World');
}
}
app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?= /* @noEscape */ $block->getHelloWorldText() ?>
完成后运行以下命令应用更改。
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento c:f
如果您处于开发者模式,请使用 -f
作为参数。
使用bin/magento deploy:mode:show
检查模式。
编码愉快!!
我一直在尝试在 Magento2 中设置一个基本模块,尽管进行了所有理想的更改,它仍然抛出 404。下面是与模块相关的代码。我的供应商名称是 Chirag,模块名称是 HelloWorld。
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Chirag_HelloWorld" schema_version="0.0.1" setup_version="0.0.1">
</module>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/etc/frontend/route.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<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="Chirag_HelloWorld" />
</route>
</router>
</config>
/var/www/html/magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
*
*
* @return void
*/
public function execute()
{
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();
}
}
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
}
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_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">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="helloworld.phtml" />
</referenceContainer>
</body>
</page>
/var/www/html/magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<h1> test hello to Magento 2 !! </h1>
如有任何帮助,我们将不胜感激。
首先尝试将 route.xml
重命名为 routes.xml
,看看是否能解决问题。
接下来尝试更改控制器中的代码,尝试更改 company/module 名称:
<?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();
}
}
同样在您的 helloworld_index_index.xml
中,您可以尝试将模板装饰更改为:
template="YourCompany_ModuleName::templatename.phtml"
最后,您可以尝试将 module.xml
setup_version
声明更改为:
setup_version="2.0.0"
希望对您有所帮助!
以下更改为我带来了正确答案:
索引控制器 - magento2/app/code/Chirag/HelloWorld/Controller/Index/Index.php
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* Show Contact Us page
*
* @return void
*/
public function execute()
{
$this->_view->loadLayout();
$this->_view->getLayout()->getBlock('helloworld');
$this->_view->renderLayout();
}
}
阻止 - magento2/app/code/Chirag/HelloWorld/Block/Question.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Block;
use Magento\Framework\View\Element\Template;
/**
* Main contact form block
*/
class Question extends Template
{
/**
* @param Template\Context $context
* @param array $data
*/
public function __construct(Template\Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
}
布局文件 - magento2/app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\Question" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml">
</block>
</referenceContainer>
</body>
</page>
查看模板 - magento2/app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?php echo 'helloworld view'; ?>
<h1> Hello World </h1>
您需要按照以下步骤解决:
您必须将
route.xml
更改为routes.xml
运行设置升级命令:
php bin/magento s:up
转到
your_base_link/helloworld/index/index
输入你的结果
首先,将route.xml
重命名为routes.xml
app/code/Chirag/HelloWorld/etc/frontend/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="helloworld" frontName="helloworld">
<module name="Chirag_HelloWorld" />
</route>
</router>
</config>
索引控制器 - app/code/Chirag/HelloWorld/Controller/Index/Index.php
不能在execute
方法中使用__construct
,这两个应该分开。
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Chirag\HelloWorld\Controller\Index;
use Magento\Framework\App\Action\Action;
class Index extends Action
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Framework\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
/**
* @return mixed
*/
public function execute()
{
return $this->resultPageFactory->create();
}
}
在布局中调用模板时应使用以下格式xml。
例如:Chirag_HelloWorld::helloworld.phtml
app/code/Chirag/HelloWorld/view/frontend/layout/helloworld_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">
<body>
<referenceContainer name="content">
<block class="Chirag\HelloWorld\Block\HelloWorld" name="helloworld" template="Chirag_HelloWorld::helloworld.phtml" />
</referenceContainer>
</body>
</page>
app/code/Chirag/HelloWorld/Block/HelloWorld.php
<?php
namespace Chirag\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
public function getHelloWorldText()
{
return __('Hello World');
}
}
app/code/Chirag/HelloWorld/view/frontend/templates/helloworld.phtml
<?= /* @noEscape */ $block->getHelloWorldText() ?>
完成后运行以下命令应用更改。
bin/magento setup:upgrade
bin/magento setup:static-content:deploy -f
bin/magento c:f
如果您处于开发者模式,请使用 -f
作为参数。
使用bin/magento deploy:mode:show
检查模式。
编码愉快!!