Symfony2.8.如何从发布请求中获取数据

A6B*_*uka 8 php post json save symfony

如何从控制器中的POST请求接收数据?我不用树枝.

public function newAction(Request $request)
{
   //when I use
   $content = $request->getContent();
   // as result I see "string" with need field and value. It's not json
   // array
   // value like
   /* 
      string '------WebKitFormBoundaryI12ukQBs3HdmPjvh
      Content-Disposition: form-data; name="title"

      "valuefortitle"
      ------WebKitFormBoundaryI12ukQBs3HdmPjvh
      Content-Disposition: form-data; name="name"

      "supername"
      ------WebKitFormBoundaryI12ukQBs3HdmPjvh--
      ' (length=253)
      */
}
Run Code Online (Sandbox Code Playgroud)

或者我如何序列化(转换)将数据发布到对象或数组

我使用Postman发送请求,标题为"Content-Type:application/json".

你能告诉我如何保存文件(图像)吗?

Ima*_*iev 9

您可以使用POST请求:

$request->request->get('data');
Run Code Online (Sandbox Code Playgroud)

对于GET请求:

$request->query->get('data');
Run Code Online (Sandbox Code Playgroud)

对于FILE查询:

$request->files.
Run Code Online (Sandbox Code Playgroud)

并提出你的问题How to save image?,你必须创建uploads -> excels:

$dir = $this->get('kernel')->getRootDir() . '/../web/uploads/images/';
$name = uniqid() . '.jpeg';

foreach ($request->files as $uploadedFile) {
    $uploadedFile->move($dir, $name);
}
$file = $this->get('kernel')->getRootDir() . "/../web/uploads/images/" . $name;

if (file_exists($file)) {
    echo "Successfully saved";
}
Run Code Online (Sandbox Code Playgroud)