2014-02-26 39 views
3

我正在使用FOS Rest软件包来构建Api,问题是每次尝试返回任何东西时都会收到错误消息:“无法找到模板”我真的想渲染一个模板,但要序列化我有的实体。当使用FOSRestBundle返回响应时无法找到模板

这里是我的代码:

的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("[email protected]"); 

      return $user; 
     } 
    } 

和我应用程序/ 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" 
     } 
    } 
} 
+0

可能重复的[FOS rest bundle:无法找到模板](http://stackoverflow.com/questions/18010784/fos-rest-bundle-unable-to-find模板) –

回答

2

我想你使用了错误的控制器,请尝试使用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("[email protected]"); 

      return $user; 
     } 
    } 

欲了解更多信息看一看在here

而且你的路由应该这样来定义。

的routing.yml

users: 
    type:  rest 
    resource: Acme\ApiBundle\Controller\UsersController 

如果你坚持作为documentation解释规则FOSRest可以自动为您生成的路由。

请花些时间阅读documentation,因为看起来很明显,如果我查看自己的代码,则不会。

+0

嗨,我试着改变父控制器类到FOSRestController,但是,错误仍然存​​在,然后我意识到您的答案与我写的“postUserAction”而不是“postAction”不同。 如果我改变了,我得到的“控制器的URI \” \ /用户\“是不是可调用的”错误 – danielrvt

+0

我改变了我的答案......并再次请务必阅读[文件](https://开头github.com/FriendsOfSymfony/FRESBestBundle/blob/master/Resources/doc/index.md) –

+0

其实我没看过这些文档,但没有一个为我工作,自动生成的路线没有找到......我是这样做,只是浪费时间,我最终使用简单的交响乐。 再说,如果我坚持到文档,没有FOSRestController提及,但FosRestController和儿子对...对你的语气感谢;)没有否决然而,) – danielrvt

1

我知道你在评论中说你放弃了,但如果你还想建立一个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一个真正的好文章:

http://williamdurand.fr/2012/08/02/rest-apis-with-symfony2-the-right-way/

4

这是因为你”可能使用浏览器来测试你的API。如果你打开你的控制台检查器,你会看到你的浏览器发送给你的API的请求是什么。正如你所看到的接受头看起来像:

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,image/webp,*/*;q=0.8。您的浏览器不知道你调用一个REST API,什么是:-)

首选格式要测试API,我建议您使用合适的客户端。如果您使用的是Chrome,请查看Advanced REST client扩展。

相关问题