2016-02-23 33 views
0

我试图设置一个StreamedResponse,以便我可以导出一个csv文件。但除了静态数组之外,我无法获得任何其他内容。Symfony2 StreamedResponse不会允许我循环或设置变量

此功能不起作用

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来执行某些有用的操作?

回答

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