【问题标题】:FosRestBundle mixing twig and jsonFosRestBundle 混合树枝和 json
【发布时间】:2016-08-02 22:24:10
【问题描述】:

我有一个使用 symfony 的完整工作项目,我需要在其中添加一些 rest api。

我正在使用 FosRestBundle,它已经使用以下配置在我的本地主机中运行:

# 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

当我将此代码(在我的本地主机中完全运行)放入 prod 环境中时,其余的 api 继续工作,但我所有的其他控制器只是返回一个“文本”而不​​是 html。我在屏幕上看到的是代码,而不是我的模板。

有人知道是什么导致了这种行为吗?

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

}

谢谢。

【问题讨论】:

    标签: php twig symfony fosrestbundle


    【解决方案1】:

    例如,如果您的所有 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 }
    

    【讨论】:

      猜你喜欢
      • 2018-06-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2019-05-11
      • 2012-09-16
      • 1970-01-01
      • 2022-01-02
      • 2014-11-03
      相关资源
      最近更新 更多