Symfony2.8.如何从发布请求中获取数据 [英] Symfony2.8. How to get data from post request

查看:121
本文介绍了Symfony2.8.如何从发布请求中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I receive data from POST request in my controller? I don't use twig.

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)
      */
}

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

Or how can I serialize(convert) Post data to object or array

我使用带有标题"Content-Type:application/json"的邮递员发送请求.

I send request using Postman with header "Content-Type: application/json".

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

And can you show me how to save file(image)?

推荐答案

您可以用于 POST 请求:

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

对于 GET 请求:

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

对于 FILE 查询:

$request->files.

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

And ask your questions How to save image?, you must create 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";
}

这篇关于Symfony2.8.如何从发布请求中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆