使用 FOSRestBundle 返回响应时无法找到模板 [英] Unable to find template when returning response with FOSRestBundle

查看:15
本文介绍了使用 FOSRestBundle 返回响应时无法找到模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 FOS Rest Bundle 来构建 Api,问题是每次我尝试返回任何内容时都会收到错误消息:无法找到模板"我真的不想渲染模板,但是序列化我拥有的实体.

I'm using FOS Rest Bundle to build an Api, the problem is that every time I try to return anything I get the error message: "Unable to find template" I don't really want to render a template, but to serialize an entity that I have.

这是我的代码:

routing.yml:

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

controller.php:

<?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;
        }
    }

和我的app/config.yml

sensio_framework_extra:
    view: { annotations: false}
    router: { annotations: true }

fos_rest:
    format_listener:
        rules:
            - prefer_extension: false
    routing_loader:
        default_format: json
    view:
        view_response_listener: force

非常感谢您的帮助!

PD:

composer.json:

{
    "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"
        }
    }
}

推荐答案

发生这种情况是因为您可能正在使用浏览器来测试您的 API.如果您打开控制台检查器,您将看到浏览器向 API 发送的请求.如您所见,Accept 标题类似于:

This happens because you're probably using a browser to test your API. If you open your console inspector you'll see what's the request your browser is sending to your API. As you can see the Accept header looks something like:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

如您所见,第一个接受的格式是text/html.这就是为什么我猜您的应用程序正在尝试返回 HTML 响应,但它找不到模板!

As you can see the first accepted format is text/html. That's why I guess your application is trying to return an HTML response, but it can't find the template!

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

So I suggest you disable the html format if you don't need it and set json as the default format.

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,image/webp,*/*;q=0.8.您的浏览器不知道您正在调用 Rest API 以及您的首选格式是什么 :-)

With such configuration you're saying that the only formats you support are json and xml and, if none is specified, use json. Now open your browser again and you'll get your resource as an XML. That's because the Accept header is still Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8. Your browser doesn't know you're calling a Rest API and what's your preferred format :-)

要测试您的 API,我建议您使用合适的客户端.如果您使用的是 Chrome,请查看高级 REST 客户端 扩展程序.

To test your API I suggest you use a proper client. If you're using Chrome then take a look at the Advanced REST client extension.

这篇关于使用 FOSRestBundle 返回响应时无法找到模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆