【问题标题】:Symfony2.8. How to get data from post requestSymfony2.8。如何从发布请求中获取数据
【发布时间】:2016-02-13 22:39:12
【问题描述】:

如何在控制器中接收来自 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)
      */
}

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

我使用 Postman 发送请求,标题为“Content-Type: application/json”。

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

【问题讨论】:

    标签: php json symfony post save


    【解决方案1】:

    您可以用于 POST 请求:

    $request->request->get('data');
    

    对于 GET 请求:

    $request->query->get('data');
    

    对于 FILE 查询:

    $request->files.
    

    问你的问题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";
    }
    

    【讨论】:

    【解决方案2】:

    转储请求中的所有数据以查找可能存在的问题:$request->request->all();

    【讨论】:

      【解决方案3】:

      $request->request->get('content');

      发帖:$request->request->get('<propertyName>');

      获取:$request->query->get('<propertyName>');

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-28
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        相关资源
        最近更新 更多