将javascript变量值发送到控制器操作

时间:2016-10-24 10:50:10

标签: javascript php typo3 fluid typo3-6.2.x

我只是尝试将一个值从javascript传递给我的控制器中的一个动作并返回该值:

这是我的控制者:

<?php
namespace Black\Test\Controller;

/**
 * CustomController
 */
class CustomController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController 
{

    /**
     * action test
     *
     * @return void
     */
    public function testAction()
    {
        $myVar = $this->request->getArgument('myVar');

        echo "myVar = ". $myVar;
    }
}

如何设置我的按钮才能使其正常工作? 我们假设我尝试传递sessionStorage Item test

这是我尝试过的:

<f:link.action controller="Custom" action="test" arguments="{myVar: sessionStorage.getItem('test')}" class="mbButton">Download</f:link.action>

我得到:The argument "arguments" was registered with type "array", but is of type "string" in view helper "TYPO3\CMS\Fluid\ViewHelpers\Link\ActionViewHelper

如果我手动设置值,例如:

<f:link.action controller="Custom" action="test" arguments="{myVar: 1}" class="mbButton">Download</f:link.action>

然后我得到1作为回复。

2 个答案:

答案 0 :(得分:1)

在控制器中:

public function testAction()
{
    $this->view->assign('test', $someVariable);

...

在流体中:

<f:link.action controller="Custom" action="test" arguments="{myVar: test}" class="mbButton" noCacheHash="1">Download</f:link.action>

我真的不知道sessionStorage是什么以及你想用它做什么。如果用testStorage替换testAction中的$someVariable,它应该可以工作。

答案 1 :(得分:1)

生成具有占位符值的链接,然后更改该占位符值将破坏为链接计算的cHash值。此外,它会在此链接和其他链接中使用相同的占位符导致cHash冲突(导致相同的链接)。

这是框架的一部分,出于安全原因,这是无法避免的。从模板中提供正确的ID;创建一个ViewHelper来处理存储,如果你需要它以某种方式分开(但要注意,如果你混合缓存和非缓存插件!)。但最重要的是,让您的链接构建遵循安全规则并包含值,以便正确计算和验证cHash。

此外,您的链接的arguments参数中存在语法错误:

arguments="{myVar: sessionStorage.getItem('test')}"

不行。如果您的sessionStorage是一个变量,并且其上的“item”实现了ArrayAccess或者具有属性“test”的getter,请使用:

arguments="{myVar: sessionStorage.item.test}"