【发布时间】:2014-01-08 10:44:09
【问题描述】:
我从 FOSRestBundle 开始。我已经添加了这个路由配置:
//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"
还有这个 config.yml
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: true
sensio_framework_extra:
view:
annotations: false
还有这个控制器:
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
);
}
}
我收到了这个错误:
[{"message":"控制器必须返回一个响应(Array(entity => 对象(Sermovi\Bundle\ManagementBundle\Entity\Transaction)) 给定)。","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 "]]},{"命名空间":"Symfony\Component\HttpKernel","short_class":"HttpKernel","class":"Symfony\Component\HttpKernel\HttpKernel","type":"->","函数":"句柄","文件":"/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\Dependen cyInjection\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":"/主页/tirengarfio/workspace/sermovi/web/app_dev.php","line":28,"args":[["object","Symfony\Component\HttpFoundation\Request"]]}]}]
编辑:
“可以”我在下面做这样的事情吗?或者因为 FOSRestBundle 正在使用 JMSSerializerBundle 我不应该这样做?
$serializedEntity = $this->container->get('serializer')->serialize($entity, 'json');
return new Response($serializedEntity);
【问题讨论】:
-
我必须说,您试图实现的目标应该是捆绑包以零配置工作的唯一方法。 FOSRestBundle 完全不符合最小惊喜原则。
标签: symfony fosrestbundle