Symfony2 StreamedResponse赢得';我不允许循环或设置变量

Symfony2 StreamedResponse赢得';我不允许循环或设置变量,symfony,Symfony,我正在尝试设置StreamdResponse,以便导出csv文件。但是,除了静态数组之外,我无法将任何东西放入流中 此功能不起作用 public function exampleCsvAction(Request $request) { $tests = array( array('one', 'two', 'three'), array('one', 'two', 'three'), array('one', 'two', 'three'

我正在尝试设置StreamdResponse,以便导出csv文件。但是,除了静态数组之外,我无法将任何东西放入流中

此功能不起作用

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        // THIS LOOP BREAKS THE RESPONSE
        foreach ($tests as $test) {
            fputcsv($handle, $test, ',');
        }

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}
但是如果我去掉foreach循环,它确实会工作

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}
调用count()函数也会破坏它

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        // THIS COUNT BREAKS THE RESPONSE
        $i = count($tests);

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}
编辑:

如果我尝试fputcsv其中一个$tests“行”,它也会中断

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        // THIS FPUTCSV BREAKS THE RESPONSE
        fputcsv($handle, $test[0], ',');

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}
但是如果数组是内联的,我可以多次放置fputcsv

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}
如果不起作用,chrome会被重定向到我的操作路径,并显示“ERR\u INVALID\u RESPONSE”消息


如何使用streamdresponse做一些有用的事情?

我发现这里遗漏了什么,我忘了使用()回调函数之外的变量

public function exampleCsvAction(Request $request)
{
    $tests = array(
        array('one', 'two', 'three'),
        array('one', 'two', 'three'),
        array('one', 'two', 'three')
    );

    $response = new StreamedResponse();
    $response->setCallback(function () use(&$tests) {
        $handle = fopen('php://output', 'w+');
        fputcsv( $handle, array('ONE', 'TWO', 'THREE'), ',');

        foreach ($tests as $test) {
            fputcsv($handle, $test, ',');
        }

        fclose($handle);
    });

    $response->setStatusCode(200);
    $response->headers->set('Content-Type', 'application/force-download');
    $response->headers->set('Content-Disposition', 'attachment; filename="example.csv"');
    return $response;
}