Symfony 返回带有FOSRestBundle的响应时找不到模板

Symfony 返回带有FOSRestBundle的响应时找不到模板,symfony,fosrestbundle,Symfony,Fosrestbundle,我使用FOS Rest Bundle构建Api,问题是每次尝试返回任何内容时,都会收到错误消息:“找不到模板”,我并不真的想呈现模板,而是想序列化我拥有的实体 这是我的密码: 路由。yml: acme_api_register: pattern: /user defaults: { _controller: AcmeApiBundle:User:post, _format: json } requirements: _method: POST <

我使用FOS Rest Bundle构建Api,问题是每次尝试返回任何内容时,都会收到错误消息:“找不到模板”,我并不真的想呈现模板,而是想序列化我拥有的实体

这是我的密码:

路由。yml:

acme_api_register:
    pattern: /user
    defaults: { _controller: AcmeApiBundle:User:post, _format: json }
    requirements:
          _method: POST
<?php

namespace Acme\ApiBundle\Controller;

use Acme\ApiBundle\Entity\Patient;
use Acme\ApiBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations\View;

//use FOS\RestBundle\View\View;

    class UserController extends Controller
    {
        /**
         * @View()
         */
        public function postAction()
        {
            $user = new Patient();

            $user->setName("Daniel");
            $user->setLastName("My lastName");
            $user->setEmail("pleasework@gmail.com");

            return $user;
        }
    }
{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "~1.2",
        "twig/extensions": "~1.0",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~2.3",
        "sensio/framework-extra-bundle": "~3.0",
        "sensio/generator-bundle": "~2.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/rest-bundle": "1.1.*",
        "jms/serializer-bundle": "0.13.*"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "symlink",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "2.4-dev"
        }
    }
}
controller.php:

acme_api_register:
    pattern: /user
    defaults: { _controller: AcmeApiBundle:User:post, _format: json }
    requirements:
          _method: POST
<?php

namespace Acme\ApiBundle\Controller;

use Acme\ApiBundle\Entity\Patient;
use Acme\ApiBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations\View;

//use FOS\RestBundle\View\View;

    class UserController extends Controller
    {
        /**
         * @View()
         */
        public function postAction()
        {
            $user = new Patient();

            $user->setName("Daniel");
            $user->setLastName("My lastName");
            $user->setEmail("pleasework@gmail.com");

            return $user;
        }
    }
{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "~1.2",
        "twig/extensions": "~1.0",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~2.3",
        "sensio/framework-extra-bundle": "~3.0",
        "sensio/generator-bundle": "~2.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/rest-bundle": "1.1.*",
        "jms/serializer-bundle": "0.13.*"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "symlink",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "2.4-dev"
        }
    }
}
我将非常感谢任何帮助

PD:

composer.json:

acme_api_register:
    pattern: /user
    defaults: { _controller: AcmeApiBundle:User:post, _format: json }
    requirements:
          _method: POST
<?php

namespace Acme\ApiBundle\Controller;

use Acme\ApiBundle\Entity\Patient;
use Acme\ApiBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\Annotations\View;

//use FOS\RestBundle\View\View;

    class UserController extends Controller
    {
        /**
         * @View()
         */
        public function postAction()
        {
            $user = new Patient();

            $user->setName("Daniel");
            $user->setLastName("My lastName");
            $user->setEmail("pleasework@gmail.com");

            return $user;
        }
    }
{
    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": {
        "psr-0": { "": "src/" }
    },
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "~1.2",
        "twig/extensions": "~1.0",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~2.3",
        "sensio/framework-extra-bundle": "~3.0",
        "sensio/generator-bundle": "~2.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "friendsofsymfony/rest-bundle": "1.1.*",
        "jms/serializer-bundle": "0.13.*"
    },
    "scripts": {
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    },
    "config": {
        "bin-dir": "bin"
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "symfony-assets-install": "symlink",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": {
            "dev-master": "2.4-dev"
        }
    }
}

我认为您使用了错误的控制器,请尝试改用FOSRestController。控制器代码应该是这样的

<?php

    namespace Acme\ApiBundle\Controller;

    use Acme\ApiBundle\Entity\Patient;
    use Acme\ApiBundle\Entity\User;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use FOS\RestBundle\Controller\Annotations\View;
    use FOS\RestBundle\Controller\FOSRestController;


    class UserController extends FOSRestController
    {
        /**
         * @View()
         */
        public function postUserAction()
        {
            $user = new Patient();

            $user->setName("Daniel");
            $user->setLastName("My lastName");
            $user->setEmail("pleasework@gmail.com");

            return $user;
        }
    }
如果您遵守中解释的规则,FOSRest可以自动为您生成路由


请慢慢看,因为如果我看一下您的代码,很明显您没有这样做。

我知道您在评论中说要放弃,但是如果您仍然想构建REST api,您应该尝试一下。 我认为您的
config.yml
是错误的。您必须指定页面格式的优先级,以确保第一个选择的是
json
(即使您在路由中编写了它,也不够)

尝试将您的配置文件更改为如下所示:

#...
fos_rest:
    view:
        view_response_listener: force
    format_listener:
        default_priorities: ['json', 'html', '*/*']
        fallback_format: json
        prefer_extension: true
#...
下面是一篇关于如何构建正确的REST Api的非常好的文章:


发生这种情况是因为您可能正在使用浏览器测试API。如果打开console inspector,您将看到浏览器向API发送的请求。正如您所看到的,Accept标题看起来像:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
如您所见,第一个接受的格式是text/html。这就是为什么我猜您的应用程序试图返回HTML响应,但它找不到模板

因此,如果不需要,我建议您禁用html格式,并将json设置为默认格式

fos_rest:
    param_fetcher_listener: true
    format_listener:
        rules:
            fallback_format: json
            prefer_extension: false
            priorities: [json, xml]
    view:
        view_response_listener: force
        formats:
            json: true
            xml: true
            jsonp: false
            rss: false
            html: false
        failed_validation: HTTP_BAD_REQUEST
在这种配置中,您的意思是您支持的唯一格式是jsonxml,如果未指定任何格式,请使用json。 现在再次打开浏览器,您将获得XML格式的资源。这是因为Accept头仍然是
Accept:text/html、application/xhtml+xml、application/xml;q=0.9,图像/webp,*/*;q=0.8
。您的浏览器不知道您正在调用Rest API以及您的首选格式:-)


为了测试您的API,我建议您使用合适的客户端。如果您使用的是Chrome,请查看扩展。

您好,我尝试将父控制器类更改为FOSRestController,但是,错误仍然存在,然后我意识到您的答案与我写的“positionAction”而不是“postAction”有不同的操作。如果我改变这个,我会得到“URI的控制器”\/user\“不可调用”错误我改变了答案。。。再说一遍,请确保你读了我确实读过的文件,但这些文件都不适合我,自动生成的路线也找不到。。。我已经完成了,这只是浪费时间,我最后用的是普通交响乐。此外,如果我坚持使用文档,就不会提到FOSRestController,而是提到FOSRestController和son。。。谢谢你的语气;)但是没有投票否决;)仅供参考:路由文档的链接已断开。更新到可能重复的