繁体   English   中英

通过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

将数据放入服务器的数据是:

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

路由看起来还可以:

admin_user_put_user放置任何一个/ 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的存在,如果它的配置,如在这里 (sensio_framework_extrafos_rest)。

另外,您可能会尝试定义一个class属性,因此它将在没有主体转换器的情况下进行映射(以查看其是否起作用):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM