将数组作为参数+ twig发送

时间:2014-04-30 08:33:53

标签: php arrays symfony twig silex

是否可以将数组作为参数发送给函数?在网上搜索过,但对我没有太大帮助。这就是我所拥有的:

在我的树枝上,我有:

<a id="exporttocsv" href="{{ path('dashboard.index', {'data': tableresults|json_encode}) }}" class="btn btn-default">Export to CSV</a>

在我的控制器中:

public function exporttocsvAction(Application $app, Request $request, $data)
{
    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");

    $output = fopen("php://output", "w");

    foreach ($data as $row)
    {
        fputcsv($output, $row, ',');
    }

    fclose($output);
    exit();
}

但是当我点击我的按钮时,我的链接会变成这样的东西,只是刷新:

  

http://thelinktomypage.com/en/dashboard?data=%5B%7B%22Code%22%3A%22010%22%2C%22Name%22%3A%22Rebecca+Zygmunt%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22014%22%2C%22Name%22%3A%22Maria+Berhe%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22021%22%2C%22Name%22%3A%22Patrick+J%5Cu00f6hnk%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22030%22%2C%22Name%22%3A%22Alisha+Helena+Storr%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22033%22%2C%22Name%22%3A%22Jan+Nicolai+Kaminski%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22034%22%2C%22Name%22%3A%22Sebastian+Mantel%22%2C%22PA%22%3Anull%2C%22PB%22%3Anull%2C%22PG%22%3Anull%2C%22GF%22%3Anull%2C%22SB%22%3Anull%2C%22VIA%22%3Anull%2C%22Blanco%22%3Anull%7D%2C%7B%22Code%22%3A%22035%22%2C%22Name%22%3A%22Sabrina+Herrmann%22%2C%22PA%22%

是否可以发送数组?或者我应该使用ajax调用并发送数据吗?

1 个答案:

答案 0 :(得分:3)

要在GET请求中传递数组,请检查此答案how to pass an array in GET in PHP?

您也可以在POST请求中执行此操作,例如使用此处所述的表单Passing arrays from HTML form to PHP

但在你的情况下,似乎最好的方法是使用ajax + json传递它然后在服务器端解码。请检查以下答案How to pass an array using PHP & Ajax to Javascript?:)