symfonyfosrestbundlesymfony-routing

symfony 3 and FOSRest Bundle Routing


I am trying to setup the symfony3 codebase with FOS REST Bundle and I end up getting no route found error

AppBundle\Resources\config\api-routing.yml

check:
    type: rest
    resource: "@AppBundle/Controller/CheckController.php"

app\config\routing.yml

app:
    type: rest
    prefix: /
    resource: '@AppBundle/Resources/config/api-routing.yml'

controller

namespace AppBundle\Controller;

use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Version;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Symfony\Component\HttpFoundation\Request;

/**
 * @RouteResource("Check")
 * @Version("v1")
 */

class CheckController extends FOSRestController
{

    public function getAction(Request $request)
    {
        $data = "Ok";
        return new JsonResponse($data);
    }
}

http://127.0.0.1:8000/v1/app/check/ return no route

I would like to integrate the versioning of rest api route. Can you share what cud be the error and how can I correct this ?

fos_rest:
    body_listener: true
    versioning:
        enabled: true
        resolvers:
            query: 
                enabled: true
                parameter_name: version
    format_listener:
        rules:
            - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
    param_fetcher_listener: true
    view:
        view_response_listener: 'force'
        formats:
            json: true

Solution

  • Here is working example.

    app\config\routng.yml

    app:
        type: rest
        resource: "@AppBundle/Resources/config/routing.yml"
        prefix: /api
    

    AppBundle\Resources\config\routing.yml

    rest_my:
        type: rest
        prefix: /v1
        resource: "@AppBundle/Controller/MyRestController.php"
        name_prefix:  api_1_
    

    Active route will be:

    http://127.0.0.1:8000/api/v1/<MyRestControllerAction>
    

    or for second controller

    AppBundle\Resources\config\routing.yml

    rest_user:
        type: rest
        prefix: /v1/user
        resource: "@AppBundle/Controller/UserRestController.php"
        name_prefix:  api_1_
    

    Active route will be:

    http://127.0.0.1:8000/api/v1/user/<UserRestControllerAction>
    

    I hope I help you... Chears.

    Update #1: Here is my complete fos_rest configuration for json format into config.yml file:

    fos_rest:
        routing_loader:
            default_format: json
            include_format: false
        body_converter:
            enabled: true
        body_listener:
            decoders:
                json: fos_rest.decoder.json
        format_listener:
            enabled: true
            rules:
                - { path: '^/api/v1/', priorities: ['json'], fallback_format: json, prefer_extension: true}
                - { path: '^/', priorities: ['json', 'html'], fallback_format: 'html' }
        serializer:
            serialize_null: true
        view:
            view_response_listener: 'force'
            templating_formats:
                html: false
        param_fetcher_listener: 'force'
    

    If you need more then that you have full documentation for fos_rest http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html