typo3 - 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 是(空的)。好像我错过了一些东西,但我无法确定在哪里:/
解决方案
您的代码中有一些常见错误,其中大部分已在此处提及,但请允许我总结一下。
扩展密钥/名称
首先,很多人混淆了扩展名和扩展键。在这种情况下,您的扩展的目录名称就是您的扩展密钥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_type
ctype
插件签名在 TypoScript 和 GET/POST 参数中进一步使用,前缀为tx_
. 在你的情况下tx_snippethighlightsyntax_feshs
。
流体和 Extbase 形式
在您的表单片段中,您已经<f:form:textfield />
使用property
标签声明了一个元素。该property
标记仅与元素上的object
andobjectName
标记一起使用<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
推荐阅读
- java - Apache poi 将大 excel 文件写入磁盘
- apache-spark - 从 collect_set 创建列值的唯一方式
- python - 将每个请求的json响应写入文件
- javascript - 延迟加载时未在移动设备上加载图像
- c# - 创建返回新动态匿名对象的表达式
- react-native - 如何在本机反应中禁用材质 UI 下拉菜单?
- angular - 默认为 '' 路由和 '**' 的 Angular 路由 404 无法按预期工作
- azure - 在 Azure Devops SSH 任务中运行“sudo”命令的安全方式
- java - 使用 gradle 和 java 在 github 项目中添加“Used by”
- python - 如何使用python将图像中方形标题的背景从黑色反转为白色?