TYPO3 8.7.13 - getArguments() return 无响应
TYPO3 8.7.13 - getArguments() return no response
\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>
SnippetsController.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 () {
});
});
请求URL
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 为(空)。好像我遗漏了什么,但我无法确定在哪里:/
$this->request->getArguments()
只会 return 以扩展/插件标识符为前缀的字段值参数,例如 tx_anything_pi1[anything]
请检查字段的 "name"-tag 是否正确。也许这些标签是错误的,因为您正在引用文本字段中对象的 "property",但没有对象绑定到您的 f:form 标签。
由于空调试的响应至少 return HTML,您的操作可能有问题。可以在浏览器中调用吗?
假设您的扩展键 (=foldername) 是 "snippet_highlight_syntax" URL 的参数通常是这样的:
tx_snippethighlightsyntax_feshs
这意味着扩展键的所有下划线都被删除了。
也许可以让它有所不同,但这不是标准的。
因此 $this->request->getArguments()
从来没有 returns 任何东西。
您必须像这样调整 url 中的参数:
index.php?id=148&type=5513&tx_snippethighlightsyntax_feshs[action]=search&tx_snippethighlightsyntax_feshs[controller]=Snippets&cHash=4662b6b5a3fa0dc4e590e8d5c90fa
在 TypoScript-Object-Browser 中,您应该可以找到具有该名称的插件:
plugin.tx_snippethighlightsyntax_feshs
首先在<f:form.textfield>
标签中使用name
属性代替property
。
然后你需要注册参数如下
public function searchAction(string $searchWords)
此外 PHP 文档块必须包含 @param string $searchWords
参数。清除安装工具中的所有缓存后,您应该得到参数。
经过多次尝试,我已将扩展程序发送到另一台计算机上进行测试,它可以正常工作。
我已经清除了所有缓存,disabled/enabled,等等。看来我的环境有问题。
感谢大家的帮助!
你的代码中有一些常见的错误,这里已经提到了大部分错误,但请允许我总结一下。
分机 key/name
首先,很多人混淆了扩展名和扩展名。您的扩展程序的目录名称是您的 扩展程序密钥 ,在本例中为 snippet_highlight_syntax
。 extension key 在整个 TYPO3 中被用作你的扩展的唯一标识符。对于 Extbase,确实出现了一个名为 extension name 的新约定以满足 PSR2 coding convention 并且主要用于 Extbase 上下文。 扩展名称 是您的扩展密钥.
的大写驼峰版本
ExtbaseFluidBook: CodingGuidelines - 这是一个旧的但仍然有效的出价
The name of the extension in UpperCamelCase. For example, if the extension-key is blog_example, then this part of the classname is 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
数据库 table 中存储的值,具体取决于插件配置 list_type
或 ctype
。
插件签名进一步用于 TypoScript 和 GET/POST 前缀为 tx_
的参数。在你的情况下 tx_snippethighlightsyntax_feshs
.
流体和 Extbase 形式
在您的表单片段中,您声明了一个带有 property
标记的元素 <f:form:textfield />
。 property
标签仅与 <f:form />
元素上的 object
和 objectName
标签一起使用,用于将值绑定到此对象属性(自动填充、验证结果等)。 ).
参见 \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.
在你的情况下,你应该正确地使用 name
而不是 property
。
更新后的表单应如下所示:
<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>
控制器参数
您应该将参数声明为控制器参数。
/**
* @param string $searchWords
*/
public function searchAction(string $searchWords = null)
{
if (is_string($searchWords)) {
// TODO: Do something here...
}
}
请注意我是如何为参数指定默认值的。这应该会抑制您收到的错误 Required argument "searchWords" is not set for...
。
这是一篇很长的文章。希望对您或其他人有所帮助。
编码愉快
\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>
SnippetsController.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 () {
});
});
请求URL
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 为(空)。好像我遗漏了什么,但我无法确定在哪里:/
$this->request->getArguments()
只会 return 以扩展/插件标识符为前缀的字段值参数,例如 tx_anything_pi1[anything]
请检查字段的 "name"-tag 是否正确。也许这些标签是错误的,因为您正在引用文本字段中对象的 "property",但没有对象绑定到您的 f:form 标签。
由于空调试的响应至少 return HTML,您的操作可能有问题。可以在浏览器中调用吗?
假设您的扩展键 (=foldername) 是 "snippet_highlight_syntax" URL 的参数通常是这样的:
tx_snippethighlightsyntax_feshs
这意味着扩展键的所有下划线都被删除了。
也许可以让它有所不同,但这不是标准的。
因此 $this->request->getArguments()
从来没有 returns 任何东西。
您必须像这样调整 url 中的参数:
index.php?id=148&type=5513&tx_snippethighlightsyntax_feshs[action]=search&tx_snippethighlightsyntax_feshs[controller]=Snippets&cHash=4662b6b5a3fa0dc4e590e8d5c90fa
在 TypoScript-Object-Browser 中,您应该可以找到具有该名称的插件:
plugin.tx_snippethighlightsyntax_feshs
首先在<f:form.textfield>
标签中使用name
属性代替property
。
然后你需要注册参数如下
public function searchAction(string $searchWords)
此外 PHP 文档块必须包含 @param string $searchWords
参数。清除安装工具中的所有缓存后,您应该得到参数。
经过多次尝试,我已将扩展程序发送到另一台计算机上进行测试,它可以正常工作。
我已经清除了所有缓存,disabled/enabled,等等。看来我的环境有问题。
感谢大家的帮助!
你的代码中有一些常见的错误,这里已经提到了大部分错误,但请允许我总结一下。
分机 key/name
首先,很多人混淆了扩展名和扩展名。您的扩展程序的目录名称是您的 扩展程序密钥 ,在本例中为 snippet_highlight_syntax
。 extension key 在整个 TYPO3 中被用作你的扩展的唯一标识符。对于 Extbase,确实出现了一个名为 extension name 的新约定以满足 PSR2 coding convention 并且主要用于 Extbase 上下文。 扩展名称 是您的扩展密钥.
ExtbaseFluidBook: CodingGuidelines - 这是一个旧的但仍然有效的出价
The name of the extension in UpperCamelCase. For example, if the extension-key is blog_example, then this part of the classname is 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
数据库 table 中存储的值,具体取决于插件配置 list_type
或 ctype
。
插件签名进一步用于 TypoScript 和 GET/POST 前缀为 tx_
的参数。在你的情况下 tx_snippethighlightsyntax_feshs
.
流体和 Extbase 形式
在您的表单片段中,您声明了一个带有 property
标记的元素 <f:form:textfield />
。 property
标签仅与 <f:form />
元素上的 object
和 objectName
标签一起使用,用于将值绑定到此对象属性(自动填充、验证结果等)。 ).
参见 \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.
在你的情况下,你应该正确地使用 name
而不是 property
。
更新后的表单应如下所示:
<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>
控制器参数
您应该将参数声明为控制器参数。
/**
* @param string $searchWords
*/
public function searchAction(string $searchWords = null)
{
if (is_string($searchWords)) {
// TODO: Do something here...
}
}
请注意我是如何为参数指定默认值的。这应该会抑制您收到的错误 Required argument "searchWords" is not set for...
。
这是一篇很长的文章。希望对您或其他人有所帮助。
编码愉快