简体   繁体   English

FosRestBundle混合树枝和json

[英]FosRestBundle mixing twig and json

I have a full working project using symfony, and i need to add some rest api inside of it. 我有一个使用symfony的完整工作项目,我需要在其中添加一些rest api。

I'm using FosRestBundle, and it's already working in my localhost using the following configs: 我正在使用FosRestBundle,它已经使用以下配置在我的localhost中工作:

# FOSRest Configuration
fos_rest:
    body_listener: true
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
           json: true

When im putting this code (that is fully working in my localhost) in the prod enviroment, the rest api continue to work, but all my other controllers is just returning me a "text" instead of html. 当我把这个代码(在我的localhost中完全正常工作)放在prod环境中时,其余的api继续工作,但我所有的其他控制器只是给我一个“文本”而不​​是html。 I'm seeing the code in the screen instead of my template. 我在屏幕上看到代码而不是模板。

Anyone knows whats causing this behavior? 有谁知道造成这种行为的原因是什么?

class TestRestController extends FOSRestController
{
    /**
     * @Rest\Get("/test")
     * @QueryParam(name="id")
     * @QueryParam(name="name")
     */
    public function indexAction(\FOS\RestBundle\Request\ParamFetcher $paramFetcher){
         ....
    }

}

Thanks. 谢谢。

For instance if all your API instances starts with /api/... , you can use this config: 例如,如果所有API实例都以/api/...开头,则可以使用此配置:

fos_rest:
    format_listener:
        rules:
          - { path: ^/api, priorities: [ json, xml ], fallback_format: json, prefer_extension: true }
          - { path: '^/', priorities: ['text/html', '*/*'], fallback_format: html, prefer_extension: true }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM