REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.
REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.
RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.
REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.). Later, we will see how much more simple REST is.
REST is not a "standard". There will never be a W3C recommendataion for REST, for example. And while there are REST programming frameworks, working with REST is so simple that you can often "roll your own" with standard library features in languages like Perl, Java, or C#.
Let's take a simple web service as an example: querying a phonebook application for the details of a given user. All we have is the user's ID.
Using Web Services and SOAP, the request would look something like this:
<?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:body pb="http://www.acme.com/phonebook"> <pb:GetUserDetails> <pb:UserID>12345</pb:UserID> </pb:GetUserDetails> </soap:Body> </soap:Envelope>
(The details are not important; this is just an example.) The entire shebang now has to be sent (using an HTTP POST request) to the server. The result is probably an XML file, but it will be embedded, as the "payload", inside a SOAP response envelope.
And with REST? The query will probably look like this:
http://www.acme.com/phonebook/UserDetails/12345
Note that this isn't the request body -- it's just a URL. This URL is sent to the server using a simpler GET request, and the HTTP reply is the raw result data -- not embedded inside anything, just the data you need in a way you can directly use.
Note how the URL's "method" part is not called "GetUserDetails", but simply "UserDetails". It is a common convention in REST design to use nouns rather than verbs to denote simple resources.
The letter analogy
A nice analogy for REST vs. SOAP is mailing a letter: with SOAP, you're using an envelope; with REST,
it's a postcard. Postcards are easier to handle (by the receiver), waste less paper (i.e., consume less bandwidth), and have a short content. (Of course, REST requests aren't really limited in length,
esp. if they use POST rather than GET.)
But don't carry the analogy too far: unlike letters-vs.-postcards, REST is every bit as secure as SOAP. In particular, REST can be carried over secure sockets (using the HTTPS protocol), and content can be encrypted using any mechanism you see fit. Without encryption, REST and SOAP are both insecure; with proper encryption in place, both are equally secure.
See rest.elkstein.org
It took me a lot of workarounds to get things working with Lumen and JWT. It all started going downhill since the installation chapter.
First the missing config_path function, then the vendor:publish command and then the illuminate/routing package.
I have managed to solve every issue that occurred and got the API working, but...
While I was writing this post, a friend of mine had an app idea and we agreed that I will write an API that is very similar to this one in the way that it uses Fractal and JWT tokens. The only difference is that I have decided to build it on Laravel 5.1.
Oh boy ... I cannot believe how much easier it was to create the same API (more advanced, with more API methods, more security, better validation) and in less time. Btw every error mentioned in this post was non existent on Laravel. It all went so smoothly and everything had just set in place.
This is the description on official Lumen website:
Lumen is the perfect solution for building Laravel based micro-services and blazing fast APIs.
Maybe this line could be a little miss guiding because of the blazing fast APIs. Mine first thought was that you are meant to build APIs with it, but once I read it one more time I got the impression that Lumen is primarily built for micro-services that in fact are small APIs. And this turns to be right. I think that one of the reasons why we don't see many tutorials on how to create an API with Lumen is because it is used for creating smaller APIs.
Don't get me wrong. If I have to write a small little API that does a thing or two and it needs to be faster I will use Lumen. But if you are building an API or something that will be the backbone of your entire application you are better off with Laravel.
There are Lots of API tutorials, but hardly any live demo's. An example says more then a 1000 words, so we collected Laravel 5 API's and made them work, so you can see them in action.
HTTP Method | Route | Corresponding Action | |
---|---|---|---|
GET | api/v1/laravel/schools | App\Http\Controllers\SchoolsController@index | See API response |
GET | api/v1/laravel/schools/create | App\Http\Controllers\SchoolsController@create | |
POST | api/v1/laravel/schools | App\Http\Controllers\SchoolsController@store | |
GET | api/v1/laravel/schools/{schools} | App\Http\Controllers\SchoolsController@show | |
GET | api/v1/laravel/schools/{schools}/edit | App\Http\Controllers\SchoolsController@edit | |
PUT | api/v1/laravel/schools/{schools} | App\Http\Controllers\SchoolsController@update | |
PATCH | api/v1/laravel/schools/{schools} | App\Http\Controllers\SchoolsController@update | |
DELETE | api/v1/laravel/schools/{schools} | App\Http\Controllers\SchoolsController@destroy |
POST | laravel/api/v1/authenticate | App\Http\Controllers\AuthenticateController@authenticate | Demo getting & storing token clientside |
POST | laravel/api/v1/authenticate | App\Http\Controllers\AuthenticateController@authenticate | Demo getting a token with jwt-auth |
GET | laravel/api/v1/jokes | App\Http\Controllers\JokesController@index | See API response |
GET | laravel/api/v1/jokes/create | App\Http\Controllers\JokesController@create | Demo CREATE |
POST | laravel/api/v1/jokes | App\Http\Controllers\JokesController@store | " |
GET | laravel/api/v1/jokes/{jokes} | App\Http\Controllers\JokesController@show | Demo SHOW |
GET | laravel/api/v1/jokes/{jokes}/edit | App\Http\Controllers\JokesController@edit | |
PUT | laravel/api/v1/jokes/{jokes} | App\Http\Controllers\JokesController@update | Demo PUT |
PATCH | laravel/api/v1/jokes/{jokes} | App\Http\Controllers\JokesController@update | Demo PATCH |
DELETE | laravel/api/v1/jokes/{jokes} | App\Http\Controllers\JokesController@destroy |
HTTP Method | Route | Corresponding Action | |
---|---|---|---|
GET | laravel/basic-authentication/todo | App\Http\Controllers\TodoController@index | See API response |
POST | laravel/basic-authentication/todo | App\Http\Controllers\TodoController@store |
HTTP Method | Route | Corresponding Action | |
---|---|---|---|
GET | laravel/larashop/api/v1/products/{id?} | Closure | See API response |
GET | laravel/larashop/api/v1/categories/{id?} | Closure | See API response |
HTTP Method | Route | Corresponding Action | |
---|---|---|---|
GET | laravel/names/api | App\Http\Controllers\NameController@index | See API response |
GET | laravel/names/api/create | App\Http\Controllers\NameController@create | |
POST | laravel/names/api | App\Http\Controllers\NameController@store | |
GET | laravel/names/api/{api} | App\Http\Controllers\NameController@show | |
GET | laravel/names/api/{api}/edit | App\Http\Controllers\NameController@edit | |
PUT | laravel/names/api/{api} | App\Http\Controllers\NameController@update | |
PATCH | laravel/names/api/{api} | App\Http\Controllers\NameController@update | |
DELETE | laravel/names/api/{api} | App\Http\Controllers\NameController@destroy |
Stateless, easier to scale: The token contains all the information to identify the user, eliminating the need for the session state. If we use a load balancer, we can pass the user to any server, instead of being bound to the same server we logged in on.
Reusability: We can have many separate servers, running on multiple platforms and domains, reusing the same token for authenticating the user. It is easy to build an application that shares permissions with another application.
Security: Since we are not using cookies, we don’t have to protect against cross-site request forgery (CSRF) attacks. We should still encrypt our tokens using JWE if we have to put any sensitive information in them, and transmit our tokens over HTTPS to prevent man-in-the-middle attacks.
Performance: There is no server side lookup to find and deserialize the session on each request. The only thing we have to do is calculate the HMAC SHA-256 to validate the token and parse its content.
Modify the Handle function in app/Http/Middleware/VerifyCsrfToken.php to:
public function handle($request, Closure $next) { //disable CSRF check on following routes $skip = array( 'user/path/xys', 'user/profile', 'my/unprotected/route' ); foreach ($skip as $key => $route) { //skip csrf check on route if($request->is($route)){ return parent::addCookieToResponse($request, $next($request)); } } return parent::handle($request, $next); }