Search code examples
symfonyfosrestbundle

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.


Solution

  • 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