In this part, we will begin to work with the REST interface. Creating a REST Api on Laravel isn’t very difficult. All we need to keep in mind is that we’re dealing with EmberJS and that we don’t want to write a new adapter from scratch. As usual, you can find the source code for this part on github.
Where to start?
That is a hard question. Ember has its own workflow and logic. If we begin to write our REST with that logic in mind we will save some time, we’ll have a nice architecture and something reusable. I think Ember has made a good choice with their REST architecture. Take a look at how Ember expects the data.
Let’s assume that we want to retrieve a user. Ember expects something like this:
{
"user": {
"firstName": "firstName",
"lastName": "lastName"
}
}
If we want to retrieve a list of users, Ember would expect a json like this:
{
"users":
[
{
"firstName": "firstPersonsName",
"lastName": "lastname"
},
{
"firstName": "secondPersonName",
"lastName": "lastName"
}
]
}
The first one requires “user”, but the second one requires “users”. The second one is plural. Ember put in some rules for that too. If you don`t specify the plural yourself by using:
Ember.Inflector.inflector.irregular('formula', 'formulae');
EmberJs will make an assumption and request “formulas”. Sometimes, it’s nice that the framework itself provides such things, but on the other hand things can get out of control if you forget these details.
Before venturing deeper with Ember, a warning: Ember is difficult and powerful. Take the time to learn how it works.
If we complicate things a little bit and put some relations between the objects, for example we say that the user has some photos. How we would output that?
Continue reading %Build REST Resources with Laravel%
more
{ 0 comments... » Build REST Resources with Laravel read them below or add one }
Post a Comment