web services with laravel

17
Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Upload: confiz

Post on 06-May-2015

7.638 views

Category:

Technology


3 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Web services with laravel

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Page 2: Web services with laravel

Boutique product development company It is amazing what you can accomplish when you have a client-centric team to deliver outstanding products.

Web Services with Laravel Abuzer Firdousi | SE

Page 3: Web services with laravel

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

Page 4: Web services with laravel

Abuzer Firdousi

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

Page 5: Web services with laravel

Abuzer Firdousi

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 extension=php_mcrypt.so >> /etc/php5/apache2/php.ini

3. /etc/init.d/apache2 restart

Page 6: Web services with laravel

Abuzer Firdousi

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

Page 7: Web services with laravel

Abuzer Firdousi

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';

});

Page 8: Web services with laravel

Abuzer Firdousi

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);

Page 9: Web services with laravel

Abuzer Firdousi

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();

Page 10: Web services with laravel

Abuzer Firdousi

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.

Page 11: Web services with laravel

Abuzer Firdousi

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) );

}

}

Page 12: Web services with laravel

Abuzer Firdousi

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', 'Namespace\FooController@method');

You may also specify names on controller routes:

Route::get('foo', array('uses' => 'FooController@method', 'as' => 'name'));

Page 13: Web services with laravel

Abuzer Firdousi

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')));

}

}

Page 14: Web services with laravel

Abuzer Firdousi

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()

{ // }

}

Page 15: Web services with laravel

Abuzer Firdousi

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);

Page 17: Web services with laravel

Questions ?

Abuzer Firdousi