从symfony 2控制器操作返回数组


Return array from symfony 2 controller action

I need to return $items array from 'http://localhost:8000/cart/viewall'
controller action. But all i get is this error.

响应内容必须是实现给定的 __toString(( "数组" 的字符串或对象。

这是我的代码,

 /**
 * @Route("/cart/viewall")
 * @Template()
 */
public function viewallAction() {
    $items = array(1 => 'item 1', 2 => 'item 2');        
    return new Response($items);
}     

如果有人可以提供解决方案,那将是非常有帮助的。

改用JsonResponse

例:

$items = array(1 => 'item 1', 2 => 'item 2');
return new JsonResponse($items);

见 http://symfony.com/doc/current/components/http_foundation/introduction.html