如何在控制器中接收来自 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)
*/
}
或者如何序列化(转换)将数据发布到对象或数组
我使用带有标题“Content-Type: application/json”的 Postman 发送请求。
你能告诉我如何保存文件(图像)吗?
您可以用于POST要求 :
$request->request->get('data');
For GET要求:
$request->query->get('data');
For 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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)