首页 > 解决方案 > TYPO3 8.7.13 - getArguments() 返回无响应

问题描述

\Templates\Snippets\Search.html

<f:form id="snippetSearchForm"
        action="search"
        controller="Snippets"
        extensionName="snippet_highlight_syntax"
        pluginName="feshs"
        name="searchSnippets"
        method="POST"
        pageType="5513">
    <f:form.textfield class="form-control" property="searchWords"/>
    <f:form.submit id="searchBtn" value="Search"/>
</f:form>

片段控制器.php

public function searchAction()
    {
        $arguments = $this->request->getArguments();
        \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($arguments);
    }

ajax.js

$("#snippetSearchForm").submit(function (event) {
    event.preventDefault();
    var form = $(this);
    var action = form.attr("action"),
        method = form.attr("method"),
        data = form.serialize();

    $.ajax({
        url: action,
        type: method,
        data: data,
        cache: false
    }).done(function (data) {
        console.log(data);
    }).fail(function () {
        ( "div.tx-feshs" ).replaceWith("errorMessage");
    }).always(function () {

    });
});

请求网址

index.php?id=148&type=5513&tx_snippet_highlight_syntax_feshs[action]=search&tx_snippet_highlight_syntax_feshs[controller]=Snippets&cHash=4662b6b5a3fa0dc4e590e8d5c90fa

我无法用getArguments(). 响应和 console.log 是(空的)。好像我错过了一些东西,但我无法确定在哪里:/

标签: typo3typo3-8.x

解决方案


您的代码中有一些常见错误,其中大部分已在此处提及,但请允许我总结一下。

扩展密钥/名称

首先,很多人混淆了扩展名扩展键。在这种情况下,您的扩展的目录名称就是您的扩展密钥snippet_highlight_syntax扩展密钥在 TYPO3 中被用作扩展的唯一标识符。Extbase 确实出现了一个新的约定,称为扩展名来满足PSR2 编码约定,并且主要用于 Extbase 上下文。扩展名是您的扩展密钥的大写驼峰式版本。

ExtbaseFluidBook: CodingGuidelines -它是旧的但仍然有效

UpperCamelCase 中的扩展名。例如,如果 extension-key 是 blog_example,那么这部分类名就是 BlogExample。

分机键:snippet_highlight_syntax
分机名称:SnippetHighlightSyntax

注意 TYPO3/Extbase 框架要求什么,键或名称——它会对你有很大帮助。


插件名称

您还声明了一个名为feshs. 根据这两种\TYPO3\CMS\Extbase\Utility\ExtensionUtility::(configure|register)Plugin()方法的 DocBlock 文档,与扩展名一样,它应该采用大写驼峰格式,例如Feshs. 它没有很好的记录,我认为它对您的应用程序没有任何负面影响,但现在您知道并且可以通过更正它来更改未来证明您的应用程序。

/**
 * ...
 *
 * @param string $extensionName The extension name (in UpperCamelCase) or the extension key (in lower_underscore)
 * @param string $pluginName must be a unique id for your plugin in UpperCamelCase (the string length of the extension key added to the length of the plugin name should be less than 32!)
 * @param array $controllerActions is an array of allowed combinations of controller and action stored in an array (controller name as key and a comma separated list of action names as value, the first controller and its first action is chosen as default)
 * @param array $nonCacheableControllerActions is an optional array of controller name and  action names which should not be cached (array as defined in $controllerActions)
 * @param string $pluginType either \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_PLUGIN (default) or \TYPO3\CMS\Extbase\Utility\ExtensionUtility::PLUGIN_TYPE_CONTENT_ELEMENT
 * @throws \InvalidArgumentException
 */
public static function configurePlugin($extensionName, $pluginName, array $controllerActions, array $nonCacheableControllerActions = [], $pluginType = self::PLUGIN_TYPE_PLUGIN)

插件签名

与您的扩展名一起,它将形成一个名为snippethighlightsyntax_feshs. 此签名是作为或取决于插件配置存储在tt_content数据库表中的值。list_typectype

插件签名在 TypoScript 和 GET/POST 参数中进一步使用,前缀为tx_. 在你的情况下tx_snippethighlightsyntax_feshs


流体和 Extbase 形式

在您的表单片段中,您已经<f:form:textfield />使用property标签声明了一个元素。该property标记仅与元素上的objectandobjectName标记一起使用<f:form />,用于将值绑定到此对象的属性(自动填充、验证结果等)。

\TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormFieldViewHelper::initializeArguments

Name of Object Property. If used in conjunction with <f:form object="...">, "name" and "value" properties will be ignored.

In your case you should properly just use name in stead of property.

Your updated form should look something like below:

<f:form id="snippetSearchForm"
        action="search"
        controller="Snippets"
        extensionName="SnippetHighlightSyntax"
        pluginName="Feshs"
        method="POST"
        pageType="5513">
    <f:form.textfield class="form-control" name="searchWords"/>
    <f:form.submit id="searchBtn" value="Search"/>
</f:form>

Controller arguments

You should declare your arguments as controller arguments.

/**
 * @param string $searchWords
 */
public function searchAction(string $searchWords = null)
{
    if (is_string($searchWords)) {
        // TODO: Do something here...
    }
}

Note how I have given the argument a default value. This should suppress the error Required argument "searchWords" is not set for... you are getting.


This was a long write up. Hopes it helps your or some others.

Happy coding


推荐阅读