The PHP Framework For Web Artisans
Introduction
Laravel is one of modern php framework that adopt many features in PHP code, provide a powerful templating engine and moves the code according to the CLI called artisan. Laravel is easy to udnerstand and it is built on top of a number of components from Symfony, one of PHP's most mature framework. To continue working with this tutorial, you need to have a composer, which basically use in PHP dependency manager. The installation can be further explore on this link. Click here
Tutorial
To get started, first off lets go to app/routes/web.php and then configure or modify according to code below :
Route::get('names', function() { return array( 1 => "Hassan", 2 => "Malik", 3 => "Syimir" ); });
Run the server using CLI command 'php artisan serve' to start remote server. Basically our local host. Then access this address http://localhost/names in our browser, you should see the output comes in array of json.
{"1":"Hassan","2":"Malik","3":"Syimir"}
However we might want to add or browse of endpoint for specific id, the coding file can be written as below :
Route::get('names/{id}', function($id) { $names = array( 1 => "Hassan", 2 => "Malik", 3 => "Syimir" ); return array($id => $names[$id]); });
So, if we browse in a specific eg. http://localhost/names/3, you should get the following in JSON format such as below :
{"3":"Syimir"}
Generally, the example shown are to create endpoints that respond to GET requests. You can change GET request to POST request, for instance, use Route::post instead of Route::get.The idea is to give exposure of Restful APIs using laravel framework and able to use resources efficiently.
No comments:
Post a Comment