laravel meetup
TRANSCRIPT
Introduction
LARAVEL FRAMEWORKWhisnu Sucitanuary
Laravel FrameworkIntroduction
• 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
I Simply found on their website :DRESTful RoutingCommand Your DataBeautiful TemplatingReady For TomorrowProven FoundationComposer PoweredGreat CommunityRed, Green, Refactor
TAYLOR OTWELL
He’sTheMan
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
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• ...
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
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
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
PHP >= 5.4MCrypt PHP Extension
Simplicity From Download To Deploy
Laravel Homestead
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
Laravel ForgeServers for Artisans | forge.laravel.com
Laravel Forge
• Instant PHP Platforms• Cloud of your choice
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');
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
Interactive shell (tinker)
Laravel Project Structure
> composer update
ROUTING & FILTERING
Routing
• Implicit Routing• Routing to Controller• Routing to Resources (REST)
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]+')
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
}
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(){...}
}
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!';}));
• Easy to understand• Expressiveness and Elegance
The Syntactic Sugar
Auth::check();Input::get();Cookie::make();Event::subscribe();Pesan::kirim(); // your own facade example
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');
FacadesEnable you to hide complex interfacec behind a simple one.
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’);
MODELVIEWCONTROLLER
•Model•View•Controller
•Eloquent ORM•Blade Engine•Controller
Model
class People extends Eloquent {
protected $table = 'people'; protected $primaryKey = 'id';
public function summary(){return truncate($this->content,100);
}}
• Query Builder• Eloquent
Controller
• Basic Controller• Controller Filter• Implicit Controller• RESTful Resource Controller
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
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
CREATE MIGRATIONROLLBACK MIGRATIONSEEDING DATA
MIGRATIONVersion Control for Database Schema
WHY
AREGOOD?
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');}
IoC Container( Inversion of Control )
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.
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
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;}
}
App::bind('keranjang_belanja',function($app){return new KeranjangBelanja(new Pelanggan,new Belanjaan);
});
$keranjangBelanja = App::make('keranjang_belanja');
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');
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');
IoC Container + Facades = Laravel
Walkthrough
Q&SQuestion and Sharing XD