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

时间:2016-01-03 09:38:20

标签: php symfony

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

Response内容必须是实现__toString()的字符串或对象,给出“array”。

这是我的代码,

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

如果有人能提供解决方案,那将会很有帮助。

1 个答案:

答案 0 :(得分:7)

请改用JsonResponse

示例:

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

请参阅http://symfony.com/doc/current/components/http_foundation/introduction.html

相关问题