Symfony2 StreamedResponse不允许我循环或设置变量

时间:2016-02-23 23:50:27

标签: symfony

我正在尝试设置StreamedResponse,以便我可以导出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其中一个$测试"行"它也打破了

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_INVALID_RESPONSE"消息

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

1 个答案:

答案 0 :(得分:0)

我看到我错过了这里,我忘了使用()回调函数之外的变量

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;
}