laravel meetup

51

Upload: whisnu-sucitanuary

Post on 04-Aug-2015

75 views

Category:

Documents


9 download

TRANSCRIPT

Page 1: Laravel Meetup
Page 2: Laravel Meetup

Introduction

LARAVEL FRAMEWORKWhisnu Sucitanuary

Page 3: Laravel Meetup

Laravel FrameworkIntroduction

Page 4: Laravel Meetup

• 10:00 – 12:00

• 12:00 – 13:00• 13:00 – 15:00

• Introduction• Installation• Create Project• Laravel Project Structure• Route and Filtering•MVC•Migration and Seeder• Break•Walkthrough

Outline

Page 5: Laravel Meetup

I Simply found on their website :DRESTful RoutingCommand Your DataBeautiful TemplatingReady For TomorrowProven FoundationComposer PoweredGreat CommunityRed, Green, Refactor

Page 6: Laravel Meetup

TAYLOR OTWELL

He’sTheMan

Page 7: Laravel Meetup

Laravel About

• Laravel is a web application framework with expressive, elegant syntax.• A modern PHP 5.3 MVC Framework• Released in July 2011• Easy to understand for both code and documentation (fun to develop

with)• Composer-based and friendly• Promote S.O.L.I.D design pattern• Supported by a thriving community• Member of PSR project• Licensed MIT

Page 8: Laravel Meetup

Some Laravel Fancy Features

• Flexible Routing• Dependency Injection / IoC Container• Event Binding• Cache Drivers• Queue Drivers• Authentication Drivers• Powerfull ActiveRecord ORM• Command-line utility• Testing Helpers• ...

Page 9: Laravel Meetup

THE PHILOSOPHY

“Any time that I have to do something that is a pain... it puts a seed in my head to

work it into Laravel”

• Reducing developmental pain points• Simple to use / expressive API• Give the developer the control over their

architecture• Grows with the developer

Page 10: Laravel Meetup

Laravel Become Mainstream

• 294 Contributors To Laravel 4.x• 7.000 Closed Issues• Most Starred PHP Project on Github• Most Watched PHP Project on Github• 1.2 Millions+ Composer Installs

Source : Laracon 2014 Keynote – Taylor Otwell

Page 11: Laravel Meetup

Installation

• Install Composer• Install Laravel• Laravel Installer using composer

composer global require "laravel/installer=~1.1"composer create-project laravel/laravel laravel-demo

• Git clone https://github.com/laravel/laravel

• Downloadhttps://github.com/laravel/laravel/archive/master.zip

Page 12: Laravel Meetup

PHP >= 5.4MCrypt PHP Extension

Page 13: Laravel Meetup

Simplicity From Download To Deploy

Page 14: Laravel Meetup

Laravel Homestead

Page 15: Laravel Meetup

Laravel Homestead

• Official Laravel Vagrant Box (http://www.vagrantup.com)

• Pre-Package for Super Fast Installation• Dead Simple Configuration File• All Your Project On a Single Box• It’s Heavenly :D

Page 16: Laravel Meetup

Laravel ForgeServers for Artisans | forge.laravel.com

Page 17: Laravel Meetup

Laravel Forge

• Instant PHP Platforms• Cloud of your choice

Page 18: Laravel Meetup

Expresiveness$articles = Article::where('author','=','otwell')

->orderBy('date','desc')->skip(5)->take(5)->get();

Auth:check();

Cache::forever(‘the_man’,’otwell’);

Redirect::to('user/login')->with('message', 'Login Failed');

Page 19: Laravel Meetup

Powerfull Command line Tool

• Based off symfony’s console component• Allow a number of application

management task to be run from the CLI (code generation, DB process,etc)• Easly customizable-write your

own

Page 20: Laravel Meetup

Interactive shell (tinker)

Page 21: Laravel Meetup

Laravel Project Structure

> composer update

Page 22: Laravel Meetup

ROUTING & FILTERING

Page 23: Laravel Meetup

Routing

• Implicit Routing• Routing to Controller• Routing to Resources (REST)

Page 24: Laravel Meetup

Implicit Routing

Route::get('news',function(){return '<p>News Page</p>';

});

Route::get('news/{id}',function($id){return 'News with id '.$id;

})->where('id','[0-9]+')

Page 25: Laravel Meetup

Routing to Controller

Route::controller('news','NewsController');Route::controller('login','LoginController');Route::controller('admin','AdminController');

class NewsController{public function getIndex(){...}public function getArticles(){...}public function postComment(){...}public function getAuthorProfile(){...} //news/author-profile

}

Page 26: Laravel Meetup

Route to Resources (REST)

Route::resource('news','NewsController');

class NewsController{public function index(){...}public function create(){...}public function store(){...}public function show(){...}public function edit(){...}public function update(){...}public function destroy(){...}

}

Page 27: Laravel Meetup

Route Filtering

Route::filter('old', function(){ if (Input::get('age') < 200) { return Redirect::to('home'); }});

Route::get('user', array('before' => 'old', function(){ return 'You are over 200 years old!';}));

Page 28: Laravel Meetup

• Easy to understand• Expressiveness and Elegance

The Syntactic Sugar

Auth::check();Input::get();Cookie::make();Event::subscribe();Pesan::kirim(); // your own facade example

Page 29: Laravel Meetup

Static Method !?

App:bind('mailer',function(){return new Mailer();

});

Route::filter('auth',function(){if(Auth::guest())

return Redirect::action('AuthController@getLogin');});

Route::model('user','User');

Page 30: Laravel Meetup

FacadesEnable you to hide complex interfacec behind a simple one.

Page 31: Laravel Meetup

What Really Happening?// This CommandRoute::get(‘/’,’HomeController@getIndex’);

// Is doing this.$app->make(‘router’)->get(‘HomeController@getIndex’);

// This CommandInput::get(‘email’);

// Is doing this.$app->make(‘request’)->get(‘email’);

// This Command$appName = Config::get(‘application.name’);

// Is doing this.$fileSystem = new Filesystem(...);$fileLoader = new Fileloader($fileSystem);$config = new Config($fileLoader,’dev’);$appName = $config->get(‘application.name’);

Page 32: Laravel Meetup

MODELVIEWCONTROLLER

Page 33: Laravel Meetup

•Model•View•Controller

•Eloquent ORM•Blade Engine•Controller

Page 34: Laravel Meetup

Model

class People extends Eloquent {

protected $table = 'people'; protected $primaryKey = 'id';

public function summary(){return truncate($this->content,100);

}}

Page 35: Laravel Meetup

• Query Builder• Eloquent

Page 36: Laravel Meetup

Controller

• Basic Controller• Controller Filter• Implicit Controller• RESTful Resource Controller

Page 37: Laravel Meetup

View

• Blade is a quick and relatively simple template engine for rendering you views.• Features many of the core functions found in other popular engines

such as twig and smarty• Since blade views are PHP scripts, you can use PHP code directly in

your templates (with caution)• Inheritance-driven to allow for multiple layouts, sections, etc

Page 38: Laravel Meetup

Layout

<html><head></head><body>

<header>This is Header</header><section class="content">

@yield('content')</section><footer>copyright@made by cool me</footer>

</body></html>

@extends('layout')@section('content')

<p>This is partial content</p>@stop

layout.blade.php

home.blade.php

Page 39: Laravel Meetup

CREATE MIGRATIONROLLBACK MIGRATIONSEEDING DATA

Page 40: Laravel Meetup

MIGRATIONVersion Control for Database Schema

WHY

AREGOOD?

Page 41: Laravel Meetup

Schema Builder

public function up(){

Schema::create('people', function(Blueprint $table){

$table->increments('id');$table->string('first_name');$table->string('last_name');$table->string('role')->default('star');$table->string('bio')->nullable();

});}

public function down(){

Schema::drop('people');}

Page 42: Laravel Meetup

IoC Container( Inversion of Control )

Page 43: Laravel Meetup

Quick word on Dependencies

• A dependency is when one component (typicaly an object) relies on another for its operation.• Dependencies should be passed (injected) into an object via its

constructor or setter methods and not instatiated inside it• Directly instatiating dependencies is considered bad practise as it

reduces scalability and flexibility, plus it make objects difficult to test in isolation.

Page 44: Laravel Meetup

Inversion of Control

public function sendEmail(){$mailer = new Mailer();$mailer->send();

}

public function sendEmail(){$mailer = App::make(‘mailer’)$mailer->send();

}

Hard-coded source dependecy

IoC Resolution

Page 45: Laravel Meetup

Dependency Example

class Computer{protected $processor;public function __construct(){

$this->processor = new ProcessorIntelI7();}

}

class Computer{protected $processor;public function __construct(ProcessorInterface $processor){

$this->processor = $processor;}

}

Page 46: Laravel Meetup

App::bind('keranjang_belanja',function($app){return new KeranjangBelanja(new Pelanggan,new Belanjaan);

});

$keranjangBelanja = App::make('keranjang_belanja');

Page 47: Laravel Meetup

Singletons

App::singleton('user_session',function($app){return new UserSession($_SESSION['REMOTE_ADDR']);

});

// First call to creates the object...$userSession = App::make('user_session');

// Second call just fetches...$userSession = App::make('user_session');

Page 48: Laravel Meetup

IoC Automatic Dependency Resolution

If you type-hint the dependencies passed into a constructor then Laravel can do the rest!

class Computer{protected $processor;public function __construct(ProcessorInterface $processor){

$this->processor = $processor;}

}

App::bind('ProcessorInterface','ProcessorIntelI7');

$computer = App:make('Computer');

Page 49: Laravel Meetup

IoC Container + Facades = Laravel

Page 50: Laravel Meetup

Walkthrough

Page 51: Laravel Meetup

Q&SQuestion and Sharing XD