7

I am developing an application using Symfony2 with fos-restbundle. I would like to create some API routes and also some regular routes (exactly one for AngularJS front-end). This is my fos_rest configuration (and a few configuration lines from 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

As you can see i have view_response_listener enabled and view annotations disabled. I can't find the way to define "regular" (not REST) route (and view) for index action (neccesary for AngularJS). Keep getting an error:

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 

I would appreciate any help with this.

probertgajda
  • 73
  • 1
  • 4

2 Answers2

13

You can add additional rule for your index page(for example):

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

Read docs about format listener: http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html

Artem Zhuravlev
  • 788
  • 6
  • 12
  • Working! Thank you very much! – probertgajda Nov 22 '15 at 20:40
  • I have another problem connected with previous one. There are many errors like this: "NetworkError: 406 Not Acceptable - http://crmproject.localhost/bundles/app/css/crm.css". I can't load any JS/CSS. Same reason as in main question, but same solution doesn't help. – probertgajda Nov 23 '15 at 10:02
  • I've tried such solution, but still getting that error. I've also changed "fallback_format" to null following to docs, but no results. Still getting "The server returned a "406 Not Acceptable". It's weird, because request's headers from Fiddler looks fine: _Accept: */* Accept-Language: pl,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate_ – probertgajda Nov 23 '15 at 11:39
  • Thats strange because my server(nginx + php5fpm ) is serving this right and I get 200 on assets. – Artem Zhuravlev Nov 23 '15 at 13:12
  • I had the same issue with Symfony 4.3.3 and it worked. Here is actual doc link: https://symfony.com/doc/master/bundles/FOSRestBundle/format_listener.html – bart Aug 13 '19 at 05:51
6

As suggested in the official docs you can also disable the format listener for the "normal" part of the site (not the APIs):

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
StockBreak
  • 2,857
  • 1
  • 35
  • 61