While developing a
good Restful API takes a load of thought and planning, great to have
application frameworks that have been developed with REST in mind,
the nitty-gritty of implementation has never been simpler. This
how-to explores RESTful API implementation by Laravel Track this API, an object-based MVC framework for PHP
which is becoming one of the language's most demanding frameworks.
Why Laravel?
There are numerous great PHP frameworks that Laravel Development Company can use to create strong PHP applications. Laravel, which is almost new, has easily gained a dedicated following. And it's not typical to understand why. Created on top of a lot of components from Symfony, one of PHP's most mature frameworks, Laravel uses many of PHP's newest features, is highly thoughtful, and features powerful components, counting a great templating engine, ORM and command line interface namely Artisan.All of this gives creators a full toolkit for creating applications of all shapes and sizes, and makes Laravel a joy to work with.
Installation & Assumptions
Installing Laravel is simple and can be done by Composer, the generally used PHP dependency manager. The Laravel documentation involves detailed installation instructions. This how-to assumes that you have a good knowledge of PHP and the model of MVC, installed Laravel on a Laravel development server, and have habitual yourself with Laravel's folder structure and conventions.Getting Started
With Laravel, it is possible to develop the simplest of APIs in a few minutes. If we open the app/routes.php file, we can very easily create endpoints for resources using Closures. For example, add the following to the routes.php:Route::get('names', function() { return array( 1 => "Rose", 2 => "Smith", 3 => "Mark" ); });
Assuming Laravel is installed locally and usable through a web server
running on local-host, you should observe that the application shows
our array as a JSON-encoded string:
{"1":"Rose","2":"Smith","3":"Mark"} Route::get('names/{id}', function($id) { $names = array( 1 => "Rose", 2 => "Smith", 3 => "Mark" ); return array($id => $names[$id]); });If we browse to http://localhost/names/2, you should receive the following JSON response:
{"2":"Smith"}Thus far, we've built endpoints that return to GET requests. As you might imagine, we can just as easily revert to other types of requests. To revert to a POST request, for example, just take Route::post instead of Route::get. Laravel also makes it possible to revert to a request regardless of type using Route::any.
Undoubtedly, the examples above are very easy. We're not doing any approval of input or error handling, and we aren't recover our data from a real database. But as you can already see, Laravel makes it simple to jump right into creating RESTful APIs without first having to revive the wheel.
Resource Controllers
In the above instances, we use Closure callbacks to represent responses directly in our routes.php file. Of course, when creating a real application, you'll possibly want to send your response logic into controllers to keep your code well-organized.As you rise familiarity with Laravel, you'll quickly notice that its creator, has made it incredibly simple and efficient to implement common performance and perform certain tasks. This is true when it comes to create RESTful applications.
We could represent routes for all of the actions associated with a resource while implementing a RESTful API. Or we could allow Laravel developer to do the heavy lifting for us and optimize Resource Controllers.
To initiate, we'll use Laravel's command line tool, Artisan, to create a new controller by typing the following at the command line:
php artisan controller:make NameControllerThis organize a file, NameController.php in the app/controllers directory, which we'll take a look at in a minute.