The Angular method, post is defined as:
this.http.post(urlHaciaDondeEnviarLosDatos, datosAEnviar, cabecerasAdicionales)
Being:
Why?
Because your frontEnd ( Angular ) is written in the JavaScript language. Whereas your backEnd ( Laravel ), is written in the PHP language.
If you want to send an object JavaScript to PHP , you can not do it directly because they are different programming languages. Therefore, do:
this.http.post(..., hero, ...)
It does not make much sense, since your hero variable is referencing a JavaScript object. Reaching Laravel an empty object ( []
) that is the scenario you describe.
So, how to send data from one language to another?
You can easily send plain text from one language to another. Therefore one solution is to convert your object from JavaScript to Text , then send that text and receive it in Laravel. Finally, convert that Text to a PHP object.
In summary:
Convert Javascript Object to Text .
Send this text from JavaScript (Angular).
Receive that text with PHP (Laravel).
Convert Text to PHP Object .
But Text can be anything. The good thing is that certain text formats were standardized for the exchange of data. XML is one of them, JSON is the most simple and popular (In turn, this format is a subset of another called YAML ).
To convert a Object to Text to JavaScript
Usas:
JSON.stringify(objeto);
Therefore, when you are doing JSON.stringify(hero)
, JavaScript is converting your hero object to this text:
{
"id":42,
"name":"SkyDog",
"power":"Fetch_any_object_at_any_distance",
"alterEgo":"Leslie_Rollover"
}
Then Angular sends this text to your server.
Laravel receives this. And when you show the content, you see that it is that text that you are receiving. ( Although without the spaces, of course )
But, if you stay there, you would be missing a step:
Convert that text to an object that PHP can handle.
You get this by using a PHP function called json_decode
. So, to convert that text into a PHP Object , you could do it like this:
$hero = json_decode($request->getContent());
Saving in the variable $hero
your data. So if you show with var_dump($hero);
you will get:
object(stdClass)#1 (4) {
["id"]=> int(42)
["name"]=> string(6) "SkyDog"
["power"]=> string(32) "Fetch_any_object_at_any_distance"
["alterEgo"]=> string(15) "Leslie_Rollover"
}
And you can access a particular property by typing, for example; $hero->name
that with var_dump
you will see: string(6) "SkyDog"
If you find it more convenient to convert that text to an Associative Array instead of an Object , you can use the second json_decode parameter :
$hero = json_decode($request->getContent(), true);
And now var_dump($hero);
will confirm that it is an Array:
array(4) {
["id"]=> int(42)
["name"]=> string(6) "SkyDog"
["power"]=> string(32) "Fetch_any_object_at_any_distance"
["alterEgo"]=> string(15) "Leslie_Rollover"
}
And you can access some property by typing $hero['name'];
.
Another way, depending on your version of Laravel ( I think 5.2 + ), accounts with a method that encapsulates json_decode . The same is:
$request->json()->all();
Now, going back to the parameters of this.http.post in Angular ...
-
Additional headers will tell you Laravel that the text you are sending is in JSON format.
You get this by writing:
this.http.post(
...,
...,
{headers: new Headers({'Content-Type': 'application/json'})}
)
Note : Headers is in import { Headers } from '@angular/http';