2016-02-13 15 views
7

Làm cách nào để nhận dữ liệu từ yêu cầu POST trong bộ điều khiển của tôi? Tôi không sử dụng cành cây.Symfony2.8. Cách nhận dữ liệu từ yêu cầu đăng

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

Hoặc làm thế nào tôi có thể serialize (convert) Đăng dữ liệu lên đối tượng hoặc mảng

tôi gửi yêu cầu sử dụng Postman với tiêu đề "Content-Type: application/json".

Và bạn có thể chỉ cho tôi cách lưu tệp (hình ảnh) không?

Trả lời

8

Bạn có thể sử dụng cho POST yêu cầu:

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

Đối GET yêu cầu:

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

Đối FILE truy vấn:

$request->files. 

Và đặt câu hỏi của bạn How to save image?, bạn phải tạo 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

Kết quả là tôi thấy "null". – A6Brgeuka

+0

Bạn có thể hiển thị biểu mẫu html 'Xem' của mình không? –

+0

bạn có thể trả lời câu hỏi của tôi cũng như Imanali Tôi đang đối mặt với vấn đề tương tự ở đây là câu hỏi của tôi https://stackoverflow.com/q/44352796/1395965 – numerah

1

Dump tất cả dữ liệu từ yêu cầu để tìm khả năng vấn đề với: $request->request->all();

0

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

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

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

Các vấn đề liên quan