PUT 数据通过 fos_rest.request_body ParamConverter - Symfony 3

PUT data through fos_rest.request_body ParamConverter - Symfony 3

我正在使用 FOS Rest Bundle 和 FOS User Bundle 开发 REST API

PUT 到服务器的数据是:

{"id":1,"email":"email@example.com","username":"adminuser","enabled":true,"locked":false,"groups" :[]}

路由看起来不错:

admin_user_put_user PUT ANY ANY /admin/users/{id}

它正在到达下面的代码:

/**
 * @ParamConverter("userData", converter="fos_rest.request_body")
 * @View(statusCode=204)
 */
public function putUserAction( $id, User $userData )
{
    $this->denyAccessUnlessGranted( 'ROLE_ADMIN', null, 'Unable to access this page!' );

    $em = $this->getDoctrine()->getManager();
    $user = $em->getRepository( 'AppBundle:User' )->find( $id );
    $user->setEmail( $userData->getEmail() );
    $user->setEmailCanonical( $userData->getEmailCanonical() );
    $user->setEnabled( $userData->isEnabled() );
    $user->setLocked( $userData->isLocked() );
    $em->flush();

}

问题是它没有用发送的数据填充 $userData

会不会是配置问题?

fos_rest:
    disable_csrf_role: ROLE_API
    view:
        view_response_listener: true
        force_redirects:
            html: true
        formats:
            json: true
        templating_formats:
            json: false
            html: true
        mime_types:
            json: ['application/json', 'application/x-json']
        failed_validation: HTTP_BAD_REQUEST
    body_listener: 
        default_format: json
    param_fetcher_listener: 
        force: true
    allowed_methods_listener: true
    access_denied_listener:
        json: true
    body_converter:
        enabled: true
        # validate: true
        # validation_errors_argument: validationErrors # This is the default value
    exception:
        enabled: true
    routing_loader:
        default_format: json
        include_format: false
    format_listener:
        rules:
            -
                # http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html
                path: ^/admin/users
                priorities: ['json', 'html', 'xml','form']
                fallback_format: ~
                prefer_extension: true
                methods: [ 'get', 'post', 'put', 'delete']
            - { path: '^/', stop: true }

我知道代码有点笨拙,欢迎提出改进建议。

保存代码中没有组数据是故意的,稍后再说。

检查是否存在 SensioFrameworkExtraBundle 以及它是否如图所示配置 heresensio_framework_extrafos_rest).

你也可以尝试定义一个 class 属性,这样它就可以在没有主体转换器的情况下进行映射(看看它是否有效):

@ParamConverter("userData", class="AppBundle:User")