Magento 2.4 自定义表单 post 正在重定向 404 或搜索
Magento 2.4 Custom form post is redirecting 404 or search
我的自定义表单有问题,应该向自定义控制器发出 post 请求,它正在将我重定向到搜索页面,我不知道为什么。
当我尝试将操作 url 粘贴到浏览器时,它正常工作。
我的表格 phtml:
<form class="quick-order-list"
method="post"
action="<?php echo $block->getFormAction(); ?>"
name="listsform"
enctype='multipart/form-data'>
<?= $block->getChildHtml('quick_order_multipleskus') ?>
<?= $block->getChildHtml('quick_order_file') ?>
<div class="quick-order-list-button">
<div class="secondary">
<button type="submit"
name="lists"
title="<?= __('Add to List') ?>"
class="action submit primary">
<span><?= __('Add to List') ?></span>
</button>
</div>
</div>
获取操作方法:
public function getFormAction()
{
return $this->getUrl('quickorder/lists/index', ['_secure' => true]);
}
控制器位于 vendor\module\Controller\Lists\Index.php 文件
<?php
declare(strict_types=1);
namespace module\vendor\Controller\Lists;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;
class Index implements HttpGetActionInterface
{
private $forwardFactory;
public function __construct(
ForwardFactory $forwardFactory
) {
$this->forwardFactory = $forwardFactory;
}
public function execute()
{
die('ello');
}
}
在 html 对我来说它看起来不错:
<form class="quick-order-list" method="post" action="http://pleasehelp.local/quickorder/lists/index/" name="listsform" enctype="multipart/form-data">
按下提交后我登陆:
/catalogsearch/result/?q=quickorder+lists+index
Magento 2.4.0,php7.3
它甚至没有进入执行函数,但是当我将 die() 放在那里时我可以在构造函数中捕获它,它正在工作。尝试了很多想法,从表单中删除字段,尝试指向其他控制器,将静态操作 url 放在那里,
对于 GET,它正在正常工作...
我开始怀疑它与我的代码无关,但项目中出现了问题,但不知道如何检查它,有人可以指出正确的方向吗?
您应该在控制器中实现 Magento\Framework\App\Action\HttpPostActionInterface
而不是 HttpGetActionInterface class
我的自定义表单有问题,应该向自定义控制器发出 post 请求,它正在将我重定向到搜索页面,我不知道为什么。 当我尝试将操作 url 粘贴到浏览器时,它正常工作。
我的表格 phtml:
<form class="quick-order-list"
method="post"
action="<?php echo $block->getFormAction(); ?>"
name="listsform"
enctype='multipart/form-data'>
<?= $block->getChildHtml('quick_order_multipleskus') ?>
<?= $block->getChildHtml('quick_order_file') ?>
<div class="quick-order-list-button">
<div class="secondary">
<button type="submit"
name="lists"
title="<?= __('Add to List') ?>"
class="action submit primary">
<span><?= __('Add to List') ?></span>
</button>
</div>
</div>
获取操作方法:
public function getFormAction()
{
return $this->getUrl('quickorder/lists/index', ['_secure' => true]);
}
控制器位于 vendor\module\Controller\Lists\Index.php 文件
<?php
declare(strict_types=1);
namespace module\vendor\Controller\Lists;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\Controller\Result\Forward;
use Magento\Framework\Controller\Result\ForwardFactory;
class Index implements HttpGetActionInterface
{
private $forwardFactory;
public function __construct(
ForwardFactory $forwardFactory
) {
$this->forwardFactory = $forwardFactory;
}
public function execute()
{
die('ello');
}
}
在 html 对我来说它看起来不错:
<form class="quick-order-list" method="post" action="http://pleasehelp.local/quickorder/lists/index/" name="listsform" enctype="multipart/form-data">
按下提交后我登陆: /catalogsearch/result/?q=quickorder+lists+index
Magento 2.4.0,php7.3
它甚至没有进入执行函数,但是当我将 die() 放在那里时我可以在构造函数中捕获它,它正在工作。尝试了很多想法,从表单中删除字段,尝试指向其他控制器,将静态操作 url 放在那里,
对于 GET,它正在正常工作...
我开始怀疑它与我的代码无关,但项目中出现了问题,但不知道如何检查它,有人可以指出正确的方向吗?
您应该在控制器中实现 Magento\Framework\App\Action\HttpPostActionInterface
而不是 HttpGetActionInterface class