Symfony2 + FOS Rest Bundle - 常规路线

Symfony2 + FOS Rest Bundle - Regular route

我正在使用 Symfony2 和 fos-restbundle 开发一个应用程序。我想创建一些 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 并禁用了 查看注释。我找不到为索引操作(AngularJS 必需)定义 "regular"(不是 REST)路由(和视图)的方法。不断出现错误:

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 

如有任何帮助,我将不胜感激。

您可以为您的索引页添加额外的规则(例如):

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

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

根据 official docs 中的建议,您还可以为站点的 "normal" 部分(不是 API)禁用格式侦听器:

Often when integrating this Bundle with existing applications, it might be useful to disable the format listener for some routes. In this case it is possible to define a rule that will stop the format listener from determining a format by setting stop to true as a rule option. Any rule containing this setting and any rule following will not be considered and the Request format will remain unchanged.

# 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