FOSRestBundle: Le contrôleur doit retourner une réponse (Array ()), étant donné

Je commence avec FOSRestBundle. J'ai ajouté cette configuration de routage:

//Sermovi/Bundle/APIBundle/Resources/config/routing.yml    
sermovi_api_homepage:
        type: rest
        resource: Sermovi\Bundle\APIBundle\Controller\DefaultController

//app/config/routing.yml     
sermovi_api:
    type: rest
    prefix:   /api
    resource: "@SermoviAPIBundle/Resources/config/routing.yml"

Et cette config.yml

fos_rest:
    routing_loader:
        default_format: json
    view:
        view_response_listener: true

sensio_framework_extra:
    view:
        annotations: false

Et ce contrôleur:

namespace Sermovi\Bundle\APIBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Response;


class DefaultController extends FOSRestController
{
    public function getArticlesAction()
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);

        return array(
            'entity' => $entity
        );
    }
}

Et j'obtiens cette erreur:

[{"message":"Le contrôleur doit retourner une réponse (Array(entité =>
Objet(Sermovi\Bundle\ManagementBundle\Entity\Transaction))
given).","class":"LogicException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2855,"args":[]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handleRaw","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2817,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2946,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel\DependencyInjection","short_class":"ContainerAwareHttpKernel","class":"Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/app/bootstrap.php.cache","line":2247,"args":[["object","Symfony\Component\HttpFoundation\Request"],["string","1"],["boolean",true]]},{"namespace":"Symfony\Component\HttpKernel","short_class":"Kernel","class":"Symfony\Component\HttpKernel\Kernel","type":"->","function":"handle","file":"/home/tirengarfio/workspace/sermovi/web/app_dev.php","line":28,"args":[["object","Symfony\Component\HttpFoundation\Request"]]}]}]

EDIT:

"Pourrait" je fais quelque chose comme ci-dessous? Ou depuis FOSRestBundle est à l'aide de JMSSerializerBundle je ne devrais pas le faire?

$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');
return new Response($serializedEntity);
  • Je dois dire que ce que vous essayez d'atteindre doit avoir été le seul et unique moyen pour le bundle de travailler avec zéro configuration. FOSRestBundle totalement ne respecte pas Moins Surprise Principe.
InformationsquelleAutor ziiweb | 2013-12-19