I'm starting with FOSRestBundle. I have added this routing configuration:
//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"
And this config.yml
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: true
sensio_framework_extra:
view:
annotations: false
And this controller:
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
);
}
}
And I'm getting this error:
[{"message":"The controller must return a response (Array(entity => Object(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:
"Could" I do something like this below? Or since FOSRestBundle is using JMSSerializerBundle I should not do it?
$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');
return new Response($serializedEntity);
There are several ways to setup your Controllers with FOSRestBundle. The way you are doing it, you must return a view. Here is a link to a rest controller on github that may help you out a bit. LiipHelloBundle has an example.
Also, I found it easiest to use the ClassResourceInterface
in my controllers. This way, I return an array, and it handles all the serialization itself. It also uses your Controller name to generate the routes that are necessary, so I don't have to manually define any routes. It is my preferred way of setting up the Controller. See the doc entry here for how that works.
If you do end up using the ClassResourceInterface
, be sure to include the following annotation for each action, it will make it so your returned array is serialized properly:
use FOS\RestBundle\Controller\Annotations as Rest;
//.....
/**
* @Rest\View()
*/
public function cgetAction() {}
You might even be able to do that with the way you are setting up the controller, but I haven't tried that before. Let us know if you go that way and it works.
UPDATE
For those who may be interested in using the FOSRestBundle without using the ClassResourceInterface
, the problem with the controller action in the question is that it does not return a view. This should work in the action:
class DefaultController extends FOSRestController
{
public function getArticlesAction()
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('SermoviManagementBundle:Transaction')->find(776);
$statusCode = 200;
$view = $this->view($entity, $statusCode);
return $this->handleView($view);
}
}