Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 + FOS Rest Bundle - Regular route

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.

like image 232
probertgajda Avatar asked Nov 22 '15 16:11

probertgajda


2 Answers

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

like image 55
Artem Zhuravlev Avatar answered Nov 17 '22 19:11

Artem Zhuravlev


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
like image 6
StockBreak Avatar answered Nov 17 '22 20:11

StockBreak