Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2.8. How to get data from post request

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

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

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

like image 687
A6Brgeuka Avatar asked Feb 13 '16 22:02

A6Brgeuka


1 Answers

You can use for POST request :

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

For GET request:

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

For FILE queries:

$request->files.

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";
}
like image 153
Imanali Mamadiev Avatar answered Sep 21 '22 15:09

Imanali Mamadiev