2016-02-13 34 views
7

如何在控制器中接收来自POST请求的数据? 我不使用树枝。Symfony2.8。如何从发布请求中获取数据

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

或如何可序列化(转换)后的数据的对象或数组

我使用邮差与标头中发送请求“内容类型:应用/ JSON”。

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

回答

8

您可以使用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"; 
} 
+0

因为我看到“null”。 – A6Brgeuka

+0

你可以显示你的'View' html表单吗? –

+0

你能回答我的问题,以及Imanali我面临类似的问题,这里是我的问题https://stackoverflow.com/q/44352796/1395965 – numerah

1

转储来自请求的所有数据,以发现可能出现的问题:$request->request->all();

0

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

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

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