user5739793
user5739793

Reputation:

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.

The Response content must be a string or object implementing __toString(), "array" given.

This is my code,

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

It would be great help if someone can supply a solution.

Upvotes: 6

Views: 12985

Answers (1)

chalasr
chalasr

Reputation: 13167

Use JsonResponse instead.

Example :

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

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

Upvotes: 9

Related Questions