Search code examples
phpsymfonyfosrestbundle

FOSRestBundle listen on prefix only


I have a Symfony application app and running, I want to run the FOSRestBundle on a prefix only /api/* I did some research and found one answer that mentioned using zone

zone:
    - { path: ^/api/* }

But I'm getting the following error:

Unable to find template "" (looked into......

This is my config:

fos_rest:
    format_listener: false
    zone:
        - { path: ^/api/* }
    view:
        view_response_listener: 'force'
        formats:
            json: true

And this is the controller:

use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\View;


class APIRestController extends FOSRestController
{
    /**
     * @Rest\Get("api/gettime")
     */
    public function getTimeAction()
    {
        $restresult = new \DateTime();

        $view = View::create($restresult, 200);
        return $view;

    }

Also:

"friendsofsymfony/rest-bundle": "2.0.0",
"jms/serializer-bundle": "^2.0"

I spent hours reading the documentation and looking online but with no luck, I just keep going in circles!


Solution

  • Fix your configuration:

    fos_rest:
        format_listener:
            rules:
                - { path: '^/api', priorities: [ 'json'], fallback_format: json, prefer_extension: true }
        zone:
            - { path: ^/api/* }
        view:
            view_response_listener: 'force'
            formats:
                json: true