控制器必须返回一个响应(给定的Object(Symfony \ Component \ Form \ Form))

时间:2014-12-06 19:09:25

标签: php symfony doctrine-orm

我们在Symfony2中使用以下捆绑包来创建RESTful API。

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

运行php composer.phar update

后,我们收到以下错误
[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 [] []

我们使用以下步骤

在我们的服务器上安装了Symfony2
//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服务器错误。

我已经将错误追溯到以下几点,我不确定它为什么不起作用。

@Annotations\View(
     templateVar = "form"
)

ClassResourceInterface类负责上述内容。

这可能与此问题有关,但此处发布的解决方案不起作用:

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

这门课有问题:

Nelmio\ApiDocBundle\Annotation\ApiDoc

有没有人也遇到过这个问题?

谢谢

2 个答案:

答案 0 :(得分:2)

您应该在文件/app/config/config.yml

中设置FOS REST配置

例如:

#/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

答案 1 :(得分:0)

尝试在开始任何新项目之前自行更新您的composer文件,以避免出现问题。

composer self-update

或者如果你的www目录中安装了composer:

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用户。

然后你必须在app / AppKernel.php

中注册你的新包
new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
new FOS\RestBundle\FOSRestBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
new Nelmio\ApiDocBundle\NelmioApiDocBundle(),

然后你的Bundle NelmioApiDocBundle需要注册自己的路由和配置:

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

# app/config/config.yml
nelmio_api_doc: ~

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