phpsymfonycontrollerresponsesymfony-2.8

Symfony Controller return array


I am trying to create a listener that configures the Response using annotations and sets the response content as the controller return.

The Controller code:

use PmtVct\PhotoBookBundle\Annotations\ResponseType;
use Symfony\Component\HttpFoundation\Request;

/**
* @ResponseType("JSON")
*/
public function home(Request $request) {
    return ['asdf' => 123];
}

But I receive the 'The controller must return a response' error.

There is a way to return an array on Controller instead a Response?


Solution

  • You are trying to do a similar thing to FOSRestBundle. Maybe consider using this bundle? It will allow:

    In case you still want to build such listener yourself - look how it's done in FOSRestBundle - https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/EventListener/ViewResponseListener.php - they are using "kernel.view" event.