Intro to Laravel 4 – A Modern PHP Framework – What are some handy features?



Intro to Laravel 4 – A Modern PHP Framework – What are some handy features?

0 0


laravel-4-intro-presentation


On Github davidrjonas / laravel-4-intro-presentation

Intro to Laravel 4

A Modern PHP Framework

Created by David Jonas / @splitretina

What is Laravel and what are its goals?

  • A web application framework with expressive, elegant syntax.
  • Strives to be accessible yet powerful
  • Emphasis on testability
  • Great documentation

What are some handy features?

Input Validation

$data = array('name' => 'David');
$rules = array('name' => 'required|between:4,50');

$validator = Validator::make($data, $rules);

if ($validator->fails())
{
    return Redirect::to('page')->withErrors($validator);
}

Query Builder

$post = DB::table('posts')->where('slug', '=', 'l4-presentation')->first();

Eloquent ORM

$posts = Post::withTag('laravel')->all();

Facades

Global scope, use them from anywhere

function logSignin()
{
    Log::info(sprintf(
        "User signed in: user_id=%s, remote_ip=%s",
        Auth::user()->id,
        Request::server()->getClientIp()
    ));
}

Inversion of Control Container

The App facade is an IoC container

App::share('KVStore\Redis', function($app)
{
    return new KVStore\Redis($app['config']->get('redis'));
});

App::bind('KVStoreInterface', 'KVStore\Redis');

$kvstore = App::make('KVStoreInterface');

Automatically resolves dependencies

Lots of others

  • Session
  • Cache
  • Event
  • Mail
  • Queue
  • Cookie
  • Auth
  • Log
  • Config
  • Blade
  • Lang
  • Paginator
  • Form Model Binding
  • string and array helpers
  • Migrations
  • Database Seeds
  • Console (artisan)
  • ...

How do I do something simple?

$ wget http://getcomposer.org/composer.phar
$ php composer.phar create-project laravel/laravel simple
$ cd simple; vi app/routes.php
Route::get("/hanky", function() {
    return "Howdy ho!";
});
$ php artisan serve &; open http://localhost:8000/hanky

How do I do something useful?

Zero to CRUD in 60 seconds

Generate Migration

$ php artisan migrate:make --create --table=guests create_guests_table
class CreateGuestsTable extends Migration { 
 
    public function up() 
    { 
        Schema::create('guests', function(Blueprint $table) 
        { 
            // You write this part
            $table->increments('id'); 
            $table->string('name'); 
            $table->string('message'); 
            $table->timestamps(); 
        }); 
    }

    public funciton down() {}
}
$ php artisan migrate

Make the Model

$ vim app/models/Guest.php
class Guest extends \Eloquent
{ 
    public $fillable = array('name', 'message');
}

Generate Controller

$ php artisan controller:make GuestbookController
class GuestbookController extends \BaseController { 
    public function index() {
        return Guest::all();
    }
    public function show($id) {
        return (Guest::find($id)) ?: Response::make(null, 404);
    }
    public function store() {
        $guest = Guest::create(Input::only(array('name', 'message')));
        return Response::make(null, 201, array('Location' => URL::to('guests', array($guest->id))));
    }
    public function update() {
        if ( ! $guest = Guest::find($id)) return Response::make(null, 404); 
        $guest->fill(Input::only(array('name', 'message')))->save();
        return Response::make(null, 205, array('Location' => URL::to('guests', array($guest->id)))); 
    }
    public function destroy($id) {
        if ( ! $guest = Guest::find($id)) return Response::make(null, 404); 
        $guest->delete();
        return Response::make(null, 204);
    }
}

Route to Controller

Route::resource('guests', 'GuestbookController');

Use it.

$ php artisan serve
$ curl -i --data "name=David&message=My%20message%20here." \
  http://localhost:8000/guests

HTTP/1.0 201 Created
Location: http://localhost:8000/guests/1

$ curl -q http://localhost:8000/guests

[{"id":"1","name":"David","message":"My message here.","created_at":"2013-11-26 17:34:27","updated_at":"2013-11-26 17:34:27"}]

Where do I learn more?