Symfony 3.4中的POST请求问题

时间:2018-02-23 16:00:32

标签: php rest symfony symfony-forms symfony-3.4

我发送以下来自postman [POST]的输入到localhost:8000

{
"UserName" : "JohnDoe",
"Password": "JohnDoe123",
"Email": "JohnDoe@email.com",
"FirstName": "John",
"LastName": "Doe"
}

这是Form:userForm.php

class userForm extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
        ->add('UserName')
        ->add('Password')
        ->add('Email')
        ->add('FirstName')
        ->add('LastName')
    ;
}


public function configureOptions(OptionsResolver $optionsResolver) {

    $optionsResolver->setDefaults(
 [

        array(

            'data_class' => user::class,
        )
  ]
    );
    }
 }

控制器:PostController.php

    public function createPostAction(Request $request) {

    $data = $request->getContent();


    $user = $this->get('jms_serializer')
    ->deserialize($data, 'AppBundle\Entity\user' ,'json');
    var_dump($user);
    exit();
    $user = new user();

    $form = $this->createForm(userForm::class, $user);

    $form->handleRequest($request);


        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

    $errors = $form->getErrors();
    $response = array(
        'message' => 'success',
        'errors' => null,
    );

    return new JsonResponse($response, 200);
}

这是我在Postman中得到的

  

object(AppBundle \ Entity \ user)#2175(6)    {    [" ID":"的appbundle \实体\用户":私人] =>    空值    ["用户名":"的appbundle \实体\用户":私人] =>    空值    ["密码":"的appbundle \实体\用户":私人] =>    空值    ["电子邮件":"的appbundle \实体\用户":私人] =>    空值    ["的firstName":"的appbundle \实体\用户":私人] =>    空值    [" lastName的":"的appbundle \实体\用户":私人] =>    空值    }

由于我是新手,我不知道该怎么做。

0 个答案:

没有答案