Magento 2 控制器 Post 抛出 404 错误
Magento 2 Controller Post throwing 404 error
我在尝试向 Magento 2 中的现有路由添加新操作时遇到问题。
我创建了一个新模块,注册了它并创建了一个扩展现有添加到购物车功能的新操作。我在一个现有的项目上工作,但不得不将它移植到一个新的安装中。现在,当我尝试 POST 到路径:<magentourl>/checkout/cart/addbulk
时,它会抛出 404。GET 请求将命中执行方法并创建日志条目。
以下是模块的组成部分
app/code/Test/Checkout/etc/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="Test_Checkout" schema_version="2.0.0" setup_version="2.0.3">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
app/code/Test/Checkout/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="checkout" frontName="checkout">
<module name="Test_Checkout" />
</route>
</router>
</config>
app/code/Test/Checkout/Controller/Cart/Addbulk.php
<?php
namespace Test\Checkout\Controller\Cart;
class Addbulk extends \Magento\Checkout\Controller\Cart\Add
{
public function execute()
{
$items = $this->getRequest()->getParam('item', []);
$this->_objectManager->get('Psr\Log\LoggerInterface')>addDebug('Posting data to addBulk');
...
return $this->_goBack();
}
}
我启用了模块,运行 bin/magento setup:upgrade
,清除了缓存,然后尝试 post。
当我执行 POST 时,它 returns 出现 404 错误,在 GET 上它按预期创建日志条目。
有没有人知道为什么这个模块没有正确迁移,或者我需要添加什么才能让它工作?
您在创建控制器后尝试过吗?
bin/magento setup:di:compile
或启用Developer mode
?
原来是环境本身坏了。我在全新安装的 magento 2 上成功安装了我的模块。
bin/magento cache:disable
清除文件夹内容
generated/code
and
generated/metadata.
同时检查 PHP-FPM 或 OPCACHE 缓存。
我在尝试向 Magento 2 中的现有路由添加新操作时遇到问题。
我创建了一个新模块,注册了它并创建了一个扩展现有添加到购物车功能的新操作。我在一个现有的项目上工作,但不得不将它移植到一个新的安装中。现在,当我尝试 POST 到路径:<magentourl>/checkout/cart/addbulk
时,它会抛出 404。GET 请求将命中执行方法并创建日志条目。
以下是模块的组成部分
app/code/Test/Checkout/etc/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="Test_Checkout" schema_version="2.0.0" setup_version="2.0.3">
<sequence>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
app/code/Test/Checkout/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="checkout" frontName="checkout">
<module name="Test_Checkout" />
</route>
</router>
</config>
app/code/Test/Checkout/Controller/Cart/Addbulk.php
<?php
namespace Test\Checkout\Controller\Cart;
class Addbulk extends \Magento\Checkout\Controller\Cart\Add
{
public function execute()
{
$items = $this->getRequest()->getParam('item', []);
$this->_objectManager->get('Psr\Log\LoggerInterface')>addDebug('Posting data to addBulk');
...
return $this->_goBack();
}
}
我启用了模块,运行 bin/magento setup:upgrade
,清除了缓存,然后尝试 post。
当我执行 POST 时,它 returns 出现 404 错误,在 GET 上它按预期创建日志条目。
有没有人知道为什么这个模块没有正确迁移,或者我需要添加什么才能让它工作?
您在创建控制器后尝试过吗?
bin/magento setup:di:compile
或启用Developer mode
?
原来是环境本身坏了。我在全新安装的 magento 2 上成功安装了我的模块。
bin/magento cache:disable
清除文件夹内容
generated/code and generated/metadata.
同时检查 PHP-FPM 或 OPCACHE 缓存。