Magento 2 -- debug.log -- main.DEBUG:操作 "Vendor\Module\Controller\Index\Post\Interceptor" 的请求验证失败

Magento 2 -- debug.log -- main.DEBUG: Request validation failed for action "Vendor\Module\Controller\Index\Post\Interceptor"

我正在实现一个自定义模块,它基本上具有与默认的 Magento 2 联系表单相同的功能。我收到一个非常恼人的错误,我似乎找不到解决方案。我的 POST 请求似乎没有通过 Magento 的请求验证,但我不确定如何找到更多具体信息。

我试图实现一个绕过 Magento CSRF 验证的 "CSRFSkip" 插件。我试过更改我的控制器扩展和实现的内容。我广泛搜索了日志。

这是我的 Magento debug.log,因为发生了错误。

[2019-10-17 19:39:16] main.DEBUG: Request validation failed for action "Vendor\Module\Controller\Index\Post\Interceptor" [] []
[2019-10-17 19:41:20] main.DEBUG: Request validation failed for action "Vendor\Module\Controller\Index\Post\Interceptor" [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'catalog.compare.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'sale.reorder.sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []
[2019-10-17 19:41:21] main.INFO: Broken reference: the 'wishlist_sidebar' element cannot be added as child to 'sidebar.additional', because the latter doesn't exist [] []

这是我的控制器。 /Vendor/Module/Controller/Index/Post.php

<?php

namespace Vendor\Module\Controller\Index;

use Magento\Framework\Controller\ResultFactory;

class Post extends \Magento\Framework\App\Action\Action implements \Magento\Framework\App\Action\HttpPostActionInterface
{
    /**
     * Post action
     *
     * @return void
     */
    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        if (!$post['name']) {
            $this->_redirect('/find-a-dealer');
            return;
        }

        $this->inlineTranslation->suspend();
        try {
            $postObject = new \Magento\Framework\DataObject();
            $postObject->setData($post);
            $error = false;

            $this->logger->debug($post['name']);
            if (!\Zend_Validate::is(trim($post['name']), 'NotEmpty')) {
                $error = true;
            }

            $this->logger->debug($post['email']);
            if (!\Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                $error = true;
            }

            $this->logger->debug($post['city']);
            if (!\Zend_Validate::is(trim($post['city']), 'NotEmpty')) {
                $error = true;
            }

            $this->logger->debug($post['state']);
            if (!\Zend_Validate::is(trim($post['state']), 'NotEmpty')) {
                $error = true;
            }

            $this->logger->debug($post['zip']);
            if (!\Zend_Validate::is(trim($post['zip']), 'NotEmpty')) {
                $error = true;
            }

            $this->logger->debug($post['part']);
            if (!\Zend_Validate::is(trim($post['part']), 'NotEmpty')) {
                $error = true;
            }

            if ($error) {
                throw new \Exception();
            }

            $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
            $transport = $this->_transportBuilder
                ->setTemplateIdentifier($this->scopeConfig->getValue(self::XML_PATH_EMAIL_TEMPLATE, $storeScope))
                ->setTemplateOptions(
                    [
                        'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE,
                        'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID,
                    ]
                )
                ->setTemplateVars(['data' => $postObject])
                ->setFrom($this->scopeConfig->getValue(self::XML_PATH_EMAIL_SENDER, $storeScope))
                ->addTo($this->scopeConfig->getValue(self::XML_PATH_EMAIL_RECIPIENT, $storeScope))
                ->setReplyTo($post['email'])
                ->getTransport();

            $transport->sendMessage();

            $this->inlineTranslation->resume();
            $this->messageManager->addSuccess(
                __('Thanks for contacting us with your comments and questions. We\'ll respond to you very soon.')
            );
            $this->getDataPersistor()->clear('find-a-dealer');
            $this->_redirect('find-a-dealer');
            return;
        } catch (\Exception $e) {
            $this->inlineTranslation->resume();
            $this->messageManager->addError(
                __('We can\'t process your request right now. Sorry, that\'s all we know.')
            );
            $this->getDataPersistor()->set('find-a-dealer', $post);
            $this->_redirect('find-a-dealer');
            return;
        }
    }

    /**
     * Get Data Persistor
     *
     * @return DataPersistorInterface
     */
    private function getDataPersistor()
    {
        if ($this->dataPersistor === null) {
            $this->dataPersistor = ObjectManager::getInstance()
                ->get(DataPersistorInterface::class);
        }

        return $this->dataPersistor;
    }
}

我希望我的 post 功能能够 运行 并发送我的电子邮件。

实际发生的是重定向到我的表单页面,并在日志中出现一些与验证相关的错误。

你能检查var、generated 和pub 文件夹的权限吗?当使用 root 用户部署更改时,权限可能会更改为 644,从而导致此类问题。

不要直接从浏览器调用 url。 将其添加到菜单操作 link 或按钮并从那里调用它。

在管理员中禁用密钥并尝试。

在我的例子中,它是 form_key。我已经明确地添加了表单键输入字段,它解决了这个问题。