Search code examples
symfony4fosrestbundle

Symfony 4.3.5 FOS\RestBundle works only default_version


I have got a problem with configuration symfony FosRest for uri versioning... My fos_rest.yaml: fos_rest:

    disable_csrf_role: ROLE_API
    param_fetcher_listener: true
    view:
        mime_types:
#            json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1', 'application/json;version=1.2']
#            xml: ['application/xml', 'application/xml']
        view_response_listener: 'force'
        formats:
            xml:  true
            json: true
        templating_formats:
            html: true
    format_listener:
        rules:
            - { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
            - { path: ^/, priorities: [ html, json, xml ], fallback_format: ~, prefer_extension: true, stop: true }
    versioning:
        enabled: true
        default_version: v1
        resolvers:
            media_type:
                enabled: true
    exception:
        codes:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
        messages:
            'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_listener: true

routes.yaml:

users:
    type:     rest
    resource: App\Controller\Api\UsersRestController
    prefix:   /api/{version}/
    name_prefix:  api_

tests:
    type:     rest
    resource: App\Controller\Api\TestsRestController
    prefix:   /api/{version}/
    name_prefix:  api_

This works:

/**
 * @Version("v1")
 */
class TestsRestController extends AbstractFOSRestController {

but this below not:

/**
 * @Version("v2")
 */
class TestsRestController extends AbstractFOSRestController {

when i change default_version: v1 to v2 works only where i declarate v2

Thanks for help


Solution

  • I found the solution.

    routes.yaml:

    controllers:
        resource: '../src/Controller/Api/'
        type:     rest
        prefix:   /api/{version}/
        name_prefix:  api_
    

    App\Controller\Api\UserController:

    /**
     * @RouteResource("User")
     */
    class UserController extends AbstractFOSRestController {
        public function cgetAction(Request $request) {
            $output = [new User(), new User()];
            $view = $this->view($output, 200);
            return $this->handleView($view);
        }
    }
    

    And in User Entity use JMS\Serializer\Annotations ex. Since or Until

    that's all ;)