Symfony2 + FOS Rest Bundle - 常规路线

pro*_*jda 6 symfony fosrestbundle

我正在使用带有fos-restbundle的Symfony2开发一个应用程序.我想创建一些API路由以及一些常规路由(只有一个用于AngularJS前端).这是我的fos_rest配置(以及来自sensio的一些配置行):

sensio_framework_extra: view: { annotations: false } router: { annotations: true } request: { converters: true } fos_rest: routing_loader: default_format: json include_format: true param_fetcher_listener: force body_listener: true allowed_methods_listener: true view: view_response_listener: 'force' formats: json: true xml: true format_listener: rules: - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true } access_denied_listener: json: true

如您所见,我启用了view_response_listener并禁用了注释.我找不到为索引操作定义"常规"(非REST)路由(和视图)的方法(AngularJS的必要).继续收到错误:

ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException: "No matching accepted Response format could be determined" at C:\wamp\www\CRMProject\vendor\friendsofsymfony\rest-bundle\EventListener\FormatListener.php line 69 
Run Code Online (Sandbox Code Playgroud)

我很感激任何帮助.

Art*_*lev 12

您可以为索引页面添加其他规则(例如):

format_listener:
    rules:
        - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
        - { path: '^/', priorities: [ 'text/html', '*/*'], fallback_format: html, prefer_extension: true }
Run Code Online (Sandbox Code Playgroud)

阅读有关格式监听器的文档:http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html


Sto*_*eak 6

根据官方文档中的建议,您还可以为网站的“正常”部分(而非API)禁用格式侦听器:

通常将此Bundle与现有应用程序集成时,禁用某些路由的格式侦听器可能会很有用。在这种情况下,可以通过将stop设置为true作为规则选项来定义一个规则,以阻止格式侦听器确定格式。包含此设置的任何规则以及之后的任何规则都将不被考虑,并且“请求”格式将保持不变。

# app/config/config.yml
fos_rest:
    format_listener:
        enabled: true
        rules:
            - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
            - { path: '^/', stop: true } # Available for version >= 1.5
Run Code Online (Sandbox Code Playgroud)