Symfony 4.3.5 FOS\RestBundle 仅适用 default_version

Symfony 4.3.5 FOS\RestBundle works only default_version

我在为 uri 版本控制配置 symfony FosRest 时遇到问题... 我的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_

这个有效:

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

但下面这个不是:

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

当我更改 default_version 时:v1 到 v2 仅在我声明 v2 的地方有效

感谢帮助

我找到了解决方案。

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);
    }
}

并且在用户实体中使用 JMS\Serializer\Annotations 例如。自或直到

就这些了;)