2014-12-06 40 views
2

我们在Symfony2中使用下面的包来创建一个RESTful API。控制器必须返回一个响应(对象(Symfony Component Form Form)给出)

new FOS\RestBundle\FOSRestBundle(), 
new JMS\SerializerBundle\JMSSerializerBundle(), 
new Nelmio\ApiDocBundle\NelmioApiDocBundle(), 

我们得到下面的错误运行PHP后composer.phar更新

[2014-12-05 10:39:41] security.INFO: Populated SecurityContext with an anonymous Token [] [] 
[2014-12-05 10:39:41] request.CRITICAL: Uncaught PHP Exception LogicException: "The controller must return a response (Object(Symfony\Component\Form\Form) given)." at /var/www/html/symfony2/app/bootstrap.php.cache line 3020 {"exception":"[object] (LogicException: The controller must return a response (Object(Symfony\\Component\\Form\\Form) given). at /var/www/html/symfony2/app/bootstrap.php.cache:3020)"} [] 
[2014-12-05 10:39:41] security.DEBUG: Write SecurityContext in the session [] [] 

我们使用以下步骤

//install symfony 
php composer.phar create-project symfony/framework-standard-edition symfony2/ 

//move composer into symfony folder 
cp composer.phar symfony2/composer.phar 
rm composer.phar 
cd symfony2 

//symfony dependencies 
php composer.phar require doctrine/doctrine-fixtures-bundle 
php composer.phar require "friendsofsymfony/rest-bundle" "@dev" 
php composer.phar require "jms/serializer-bundle" "@dev" 
php composer.phar require "nelmio/api-doc-bundle" "@dev" 

的API请求将返回500服务器在我们的服务器上安装的Symfony2错误。

由于我不确定它为什么不起作用,我已经将错误追溯到下面的点。

@Annotations\View(
    templateVar = "form" 
) 

ClassResourceInterface类负责以上操作。

这可能涉及到这个问题,但在此发布的解决方案不起作用:

FOSRestBundle: The controller must return a response (Array()) given

东西是不对的类:

Nelmio\ApiDocBundle\Annotation\ApiDoc 

有没有人有这个问题,因为好?

谢谢

+0

什么的'Annotations'别名您使用的语句? – 2014-12-06 19:25:05

+0

@WouterJ你用什么声明来表示? – pppglowacki 2014-12-08 08:18:46

回答

2

您应该设置FOS REST配置文件/app/config/config.yml

例如:

#/app/config/config.yml 
fos_rest: 
param_fetcher_listener: true 
body_listener: true 
format_listener: false 
routing_loader: 
    default_format: json 
view: 
    view_response_listener: 'force' 
    formats: 
     xml: true 
     json : true 
    default_engine: none 
    templating_formats: 
     html: true 
access_denied_listener: 
    json: true 
allowed_methods_listener: true 
+0

感谢您的回答。它像一个魅力一样工作! – medina 2015-10-11 06:57:22

0

尝试在启动任何新项目之前自行更新您的作曲文件以避免问题。

composer self-update 

,或者是安装在你的www目录作曲:

php composer.phar self-update 

,那么你可以与所有的包创建一个新的symfony项目,你需要:

composer create-project symfony/framework-standard-edition symfony2/ 
cd symfony2 
composer require doctrine/doctrine-fixtures-bundle 
composer require friendsofsymfony/rest-bundle 
composer require jms/serializer-bundle 
composer require nelmio/api-doc-bundle 

有时候,在这一点上我需要给“app/cache”或“app/bootstrap.php.cache”等目录提供读/写权限,因为我的终端使用我的用户,但apache使用_www用户。

然后,你必须注册在应用新的包/ AppKernel.php

new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(), 
new FOS\RestBundle\FOSRestBundle(), 
new JMS\SerializerBundle\JMSSerializerBundle(), 
new Nelmio\ApiDocBundle\NelmioApiDocBundle(), 

那么你的包NelmioApiDocBundle需要注册自己的路由和配置:

# app/config/routing.yml 
NelmioApiDocBundle: 
    resource: "@NelmioApiDocBundle/Resources/config/routing.yml" 
    prefix: /api/doc 

# app/config/config.yml 
nelmio_api_doc: ~ 

一切都必须在工作这一点,因为我已经这样做了。

+0

我跟着步骤,一切顺利如预期。 API文档显示没有任何问题,但当我去任何我的API调用,我仍然得到相同的500错误。 – pppglowacki 2014-12-08 08:20:43

+0

所以你认为这个问题是由于这些包而不是你自己的API代码?你能分享一些导致500错误的API调用吗? – 2014-12-08 09:23:48

相关问题