Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Web services with laravel

The session was about how to create the Restful Web Services with Laravel, a PHP framework with the minimal code. Topics discussed are:
-Laravel Philosophy
-Basic Routing
-Requests & Input
-Request Lifecycle
-Creating A Migration
-Controller Filters
-RESTful Controllers
-Eloquent ORM

Web services with laravel

  1. 1. Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
  2. 2. Web Services with Laravel Boutique product development company Abuzer Firdousi | SE It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.
  3. 3. Web Services with Laravel Content: • • • • • • • • • • • • • Laravel Philosophy Requirement Installation Basic Routing Requests & Input Request Lifecycle Controller Controller Filters RESTful Controllers Database Model using Eloquent ORM Creating A Migration Code Example Questions Abuzer Firdousi
  4. 4. Laravel Philosophy Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable, creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tas • ROR • ASP.NET MVC • Sinatra (Sinatra is a DSL for quickly creating web applications in Ruby with minimal effort) Laravel is accessible, yet powerful, providing powerful tools needed for large, robust applications. Laravel, the elegant PHP framework for web artisans Abuzer Firdousi
  5. 5. Server Requirements The Laravel framework has a few system requirements: • PHP >= 5.3.7 • MCrypt PHP Extension As of PHP 5.5, some OS distributions may require you to manually install the PHP JSON extension. When using Ubuntu, this can be done via apt-get install php5-json. MCrypt: 1. aptitude install php5-mcrypt 2. echo >> /etc/php5/apache2/php.ini 3. /etc/init.d/apache2 restart Abuzer Firdousi
  6. 6. Installation Laravel utilizes Composer to manage its dependencies. First, download a copy of the composer.phar. Via Composer Create-Project You may install Laravel by issuing the Composer create-project command in your terminal: composer create-project laravel/laravel --prefer-dist Via Download Once Composer is installed, download the latest version of the Laravel framework and extract its contents into a directory on your server. Next, in the root of your Laravel application, run the php composer.phar install (or composer install) command to install all of the framework's dependencies. This process requires Git to be installed on the server to successfully complete the installation. If you want to update the Laravel framework, you may issue the php composer.phar update command. Laravel provides a server, and we can run the server with “php artisan* serve” * command-line interface, a powerful Symfony Console component Abuzer Firdousi
  7. 7. Basic Routing Most of the routes for your application will be defined in the app/routes.php file. The simplest Laravel routes consist of a URI and a Closure callback. Basic GET Route: Route::get('/', function() { return 'Hello World'; }); Basic POST Route: Route::post('foo/bar', function() { return 'Hello World'; }); Abuzer Firdousi
  8. 8. Route Parameters Route Parameters: Route::get('user/{id}', function($id) { return 'User '.$id; }); Optional Route Parameters: Route::get('user/{name?}', function($name = null) { return $name; }); Throwing 404 Errors: There are two ways to manually trigger a 404 error from a route. First, you may use the App::abort method: App::abort(404); Abuzer Firdousi
  9. 9. Requests & Input Retrieving An Input Value $name = Input::get('name'); Retrieving A Default Value If The Input Value Is Absent $name = Input::get('name', Abuzer); Getting All Input For The Request $input = Input::all(); Abuzer Firdousi
  10. 10. Request Life Cycle The Laravel request lifecycle is fairly simple. A request enters your application and is dispatched to the appropriate route or controller. The response from that route is then sent back to the browser and displayed on the screen. Sometimes you may wish to do some processing before or after your routes are actually called. There are several opportunities to do this, two of which are "start" files and application events. Abuzer Firdousi
  11. 11. Controllers Instead of defining all of your route-level logic in a single routes.php file, you may wish to organize this behavior using Controller classes. Controllers can group related route logic into a class, as well as take advantage of more advanced framework features such as automatic dependency injection. Controllers are typically stored in the app/controllers directory, and this directory is registered in the class map option of your composer.json file by default. Here is an example of a basic controller class: class UserController extends BaseController { /** * Show the profile for the given user. */ public function showProfile($id) { $user = User::find($id); // return Response::json( array('user' => $user) ); } } Abuzer Firdousi
  12. 12. Controllers and Routes All controllers should extend the BaseController class. The BaseController is also stored in the app/controllers directory, and may be used as a place to put shared controller logic. The BaseController extends the framework's Controller class. Now, We can route to this controller action like so: Route::get('user/{id}', 'UserController@showProfile'); If you choose to nest or organize your controller using PHP namespaces, simply use the fully qualified class name when defining the route: Route::get('foo', 'NamespaceFooController@method'); You may also specify names on controller routes: Route::get('foo', array('uses' => 'FooController@method', 'as' => 'name')); Abuzer Firdousi
  13. 13. Controller Filters Filters may be specified on controller routes similar to "regular" routes: ( Kind of Request INTERCEPTOR) Route::get('profile', array('before' => 'auth', 'uses' => 'UserController@showProfile')); However, you may also specify filters from within your controller: class UserController extends BaseController { /** * Instantiate a new UserController instance. */ public function __construct() { $this->beforeFilter('auth', array('except' => 'getLogin')); $this->afterFilter('log', array('only' => array('fooAction', 'barAction'))); } } Abuzer Firdousi
  14. 14. RESTful Controllers Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method: Defining A RESTful Controller Route::controller('users', 'UserController'); The controller method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller. Next, just add methods to your controller, prefixed with the HTTP verb they respond to: class UserController extends BaseController { public function getIndex() { // } public function postIndex() { // } public function putIndex() { // } public function deleteIndex() { // } } Abuzer Firdousi
  15. 15. Eloquent ORM The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Before getting started, be sure to configure a database connection in app/config/database.php. class User extends Eloquent { protected $table = 'my_users'; } Retrieving All Models $users = User::all(); Retrieving A Record By Primary Key $user = User::find(1); Querying Using Eloquent Models $users = User::where('votes', '>', 100)->take(10)->get(); foreach ($users as $user) var_dump($user->name); Abuzer Firdousi
  16. 16. Example Repo: Route: 74842a339d50f9e4d5ae/app/routes.php?at=master Controller: 74842a339d50f9e4d5ae/app/controllers/TodoController.php?at= master Model: 74842a339d50f9e4d5ae/app/models/Todo.php?at=master Abuzer Firdousi
  17. 17. Questions ? Abuzer Firdousi

    Be the first to comment

    Login to see the comments

  • akbarwibawa

    Feb. 18, 2014
  • sefakaragoz06

    Mar. 21, 2014
  • the.big.boss

    Jun. 3, 2014
  • JDurrah

    Aug. 4, 2014
  • theoeinsten

    Aug. 26, 2014
  • peterpank89

    Oct. 20, 2014
  • wedgwood

    Feb. 3, 2015
  • BrandonNgo

    Feb. 5, 2015
  • NeoHeartless

    Feb. 6, 2015
  • VendyVecker

    Mar. 4, 2015
  • luisvar12

    Jun. 30, 2015
  • AlbertoMalacara

    Aug. 31, 2015
  • agusarimbawa2

    May. 6, 2016
  • guibernardi

    Jul. 6, 2016
  • williammdavis

    Feb. 19, 2017
  • yaroslavageichick

    May. 17, 2017
  • alaalborihi

    Jun. 3, 2017
  • mabdelaal86

    Aug. 20, 2017
  • ketsakda1

    Sep. 27, 2017
  • danandjaya

    Aug. 16, 2018

The session was about how to create the Restful Web Services with Laravel, a PHP framework with the minimal code. Topics discussed are: -Laravel Philosophy -Requirement -Installation -Basic Routing -Requests & Input -Request Lifecycle -Creating A Migration -Controller -Controller Filters -RESTful Controllers -Database -Eloquent ORM


Total views


On Slideshare


From embeds


Number of embeds