Symfony2 + FOS Rest Bundle - Regular route
Asked Answered
T

2

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.

Thury answered 22/11, 2015 at 16:49 Comment(0)
S
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

Swallowtail answered 22/11, 2015 at 17:51 Comment(5)
Working! Thank you very much!Thury
I have another problem connected with previous one. There are many errors like this: "NetworkError: 406 Not Acceptable - 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.Thury
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, deflateThury
Thats strange because my server(nginx + php5fpm ) is serving this right and I get 200 on assets.Swallowtail
I had the same issue with Symfony 4.3.3 and it worked. Here is actual doc link: symfony.com/doc/master/bundles/FOSRestBundle/…Fullfledged
R
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
Romaine answered 28/7, 2017 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.