Laravel
Clean and classy PHP framework
Presentation by Maks
What do I do:
Breakdancer (4 years)
IT support (8 years)
Web Development (3 years)
Created Bootsnipp.com, Cheatsheetr.com, Filegr.am, and more
Why Framework?
WHY Another Framework?
- Easy to start with
- Great Separation (think about working in teams)
- Expressive syntax (like Session::put(key, value))
-
Extensions (through Composer packages)
-
Testability (easy dependency injection)
-
Dependent (bootsnipp, e-commerce, CMS, and more)
- Eloquent ORM - ActiveRecord Implementation
- Easy routing (like sinatra for ruby)
- Caching built in - use APC, Memcache, Redis or DB
- Combines best practices, inspiration from RoR and CI
- Flexible MVC
- MIT license (!)
As @Rutger says
"With Laravel we can do it in one line."
Make it fun.
Eloquent Object Relational Mapping
Uses prepared statements (SQL injection defense)
works with MySQL, PostgreSQL, SQLite, SQL Server
Example of actual usage:
class Company extends Eloquent {}
$company = Company::create(array( 'address' => '155 Harbor Blvd', 'city' => 'Fullerton' ));
$company = Company::find(1); echo $company->address; // 155 Harbor Blvd
$company = Company::where('city', "Fullerton")->first();
$companies = Company::orderBy('city', 'desc')->take(10)->get();
Pagination built in! Example:
$companies = Company::orderBy('city', 'desc')->paginate(10);
Relationships
One to One
class Company extends Eloquent { public function salesperson() { return $this->hasOne('Salesperson'); } }
$salesperson = Company::find(1)->salesperson->name;
Many to One
class Company extends Eloquent { public function customers() { return $this->hasMany('Customer'); } }
$customers = Company::find(1)->customers;
Many to Many + Polymorphic
Query scopes
class User extends Eloquent { public function scopePopular($query) { return $query->where('votes', '>', 100); } public function scopeWomen($query) { return $query->whereGender('W'); } }
// Using the scope
$popularWomen = User::popular()->women()->orderBy('created_at')->get();
Routing
Super easy!
Route::get('/', function() { return "Hello PHP!"; }); Route::post('user', function() { // });
Passing parameters:
Route::get('user/{id}', function($id) { return 'User '.$id; });
Route Constraints:
Route::get('user/{name}', function($name) { // })->where('name', '[A-Za-z]+');
Routing #2
Controller Routing
Route::controller('company');
OR
Route::get('admin','AdminController@getIndex');
Route filters
Route::get('create', array('before' => 'auth|csrf', function() { // }));
Authentication
Out of the box
$credentials = array( 'username' => 'example@gmail.com', 'password' => 'secret' ); if (Auth::attempt($credentials)) { return Redirect::to('user/profile'); }
Get user's info : $email = Auth::user()->email;
Logout: Auth::logout();
Sessions
Easy to manage, easy to use
Session::put('key', 'value'); or for next request only Session::flash('key', 'value'); $value = Session::get('key'); if (Session::has('cart')) { // } Session::forget('key'); Session::flush();
Blade templating engine
Kinda like PHP-ied HTML, ex:
@if ($tags)
<p class="lead">Tags matching the search:</p> <ul class='nav nav-list'> @foreach ($tags as $tag) <li> <a href="{{ url('tags/'.$tag->slug) }}"> <span class="label">{{$tag->count}} snipps</span> {{$tag->name}} </a> </li> @endforeach @endif
HTML and Form Helpers
HTML::script('js/scrollTo.js'); // <script src="http://localhost:8000/js/scrollTo.js"></script> HTML::style('css/common.css'); // <link media="all" type="text/css" rel="stylesheet" href="http://localhost:8000/css/common.css">
Form helpers:
Form::open(); Form::text('username'); Form::password('password'); Form::checkbox('name', 'value', true); Form::file('image'); Form::select('size', array('L' => 'Large', 'S' => 'Small')); Form::submit('Click Me!'); Form::close();
Environments
bootstrap/start.php:
$env = $app->detectEnvironment(array( 'local' => array('http://localhost*', '*.dev'), 'staging' => array('http://dev.mywebsite.com*'), 'production' => array('http://mywebsite.com*'), ));
Then add environment-specific config files under
app/config
app - config - staging - database.php - session.php - production - database.php ...
Done!
Composer packages
Over 1000 Laravel-specific packages
that can be integrated for:
- Image manipulation
- oAuth login
- payments API
- accelerating development
- formatting data
- adding to Laravel's core functionality
- and more
Some cool packages:
- Former (Bootstrap forms)
- Mandrill
- Hashids
- php-loep/oauth2-client
Validation
$input = Input::all(); $rules = array( 'name' => 'required|min:3|max:32|alpha', 'age' => 'required|integer', 'image'=> 'image|max:1500', 'password' => 'confirmed'
); $v = Validator::make($input, $rules); if( $v->fails() ) { // code for validation failure :( } else { // code for validation success! }
+ 14 more types(including RegEx)
Localization
app/lang/ua/marketing.php
{{ trans('marketing.welcome' )}}
Artisan CLI
Get list of all commands:
php artisan list
key:generate Generate a secure application key.
# Seeding of the DB db:seed
# Migrations migrate:install Create the Laravel migration table. migrate:make Create a migration. migrate Run outstanding migrations. migrate:rollback Roll back the most recent migration. migrate:reset Roll back all migrations.
Community
@laravelphp on Twitter:
Great Forums and also
IRC channel #laravel
Thanks and Enjoy!
Resources:
http://manning.com/surguy - "Laravel in Action" book from Manning Publications
http://maxoffsky.com - my blog, Laravel, Backbone, web dev
http://builtwithlaravel.com - list of sites using Laravel
http://laravel-tricks.com - tips and tricks for Laravel users