Laravel For Web Artisans
Laravel For Web Artisans
www.taylorotwell.com
@taylorotwell
http://taylorotwell.com/on-community/
V 4.2.X Current Release
http://www.github.com/laravel/laravel
Google Trends
from 2004 to current
<? WHY ?>
Easy to Learn
Ready For Tomorrow
Great Community
Composer Powered
FAIL PASS
REFACTOR
INSTALLATION
It’s too easy, I don’t have to tell you.
IoC
Inverse of Control
Illuminate\Container\Container
In software engineering, inversion of control
describes a design in which custom-written
portions of a computer program receive the
flow of control from a generic, reusable library
“what you get when your program make a call”
$object = App::make(‘SomeClass’);
Hard-coded source dependency
public function sendEmail()
{
$mailer = new Mailer;
$mailer->send();
}
IoC Resolution
public function sendEmail()
{
$mailer = App::make(‘Mailer’);
$mailer->send();
}
Telling IoC what we need
App::bind(‘Bar’, ‘MockBar’);
App::bind(‘SomeClass’, function() {
return new Foo;
});
$auth = App::make(‘Auth’);
return $auth->user(); // returns user object
});
Automatic Dependency Resolution
class EmailService {
$EmailService = App::make(‘EmailService’);
Service Providers
Service Providers
use Illuminate\Support\ServiceProvider;
Illuminate/Support/Facades/Facade
What is Facade
In Laravel Context
Facade
IoC Container Object A
Class
Route::get(‘/’, ‘HomeController@getIndex’);
$app->make(‘router’)->get(‘/’, ‘HomeController@getIndex’);
Illuminate/Support/Facades/Route Illuminate/Routing/Router
Facades in Laravel
Illuminate/Support/Facades/
Illuminate/Foundation/Application
Eloquent
Talking to your data
Illuminate\Database\Eloquent\Model
How you query in plain PHP 5.4 or less (deprecated)
User::all();
What happens when you use Eloquent ORM
User::all();
Illuminate\Database\Query\Builder
DB::table(‘users’)->get();
Create, Read, Update, Delete
Post::create($data);
Post::find($id);
Post::find($id)->update($data);
Post::delete($id);
Eloquent Model Class
$post = Post::find($id);
$author = post->author;
// get the author object
$posts = Post::with(‘author’)->get();
Get Post Comments Approved
$post = Post::find($id);
$comments = $post->comments()
->where(‘approved’, true)->get();
Model with Query Scope
$published_posts = Post::published()->get();
// gets all posts published
Query Scope Example
return $q->where(‘publish’,’=‘,1);
}
}
Eloquent Relations
Eloquent Relationship
One to One
(hasOne)
Users Profile
pK: id pk: id
username fK: user_id
email url
class User
function Profile()
$this->hasOne(‘Profile’,
‘user_id’);
Eloquent Relationship
One to Many
(hasMany)
Posts Comments
pK: id pk: id
title fK: post_id
user_id user_id
class Post
function comments()
$this->
hasMany(‘Comment’);
Eloquent Relationship
Many to Many
(belongsToMany)
Users Groups
pK: id pk: id
username Name
email
Group_User
user_id
group_id
class User
function groups()
$this->
belongsToMany(‘Group’);
Eloquent Relationship
One to Many through
(hasManyThrough)
Countries Posts
pK: id pk: id
name user_id
code title
Users
body
pK: id
country_id
username
email
class Country
function posts()
$this->
hasManyThrough(‘User’);
Eloquent Relationship
Polymorphic Association
Profile
pK: id
user_id Photos
Movie preferences pk: id
pK: id file_path
title imageable_id
released imageable_type
$user->image; $movie->image;
Eloquent Relationship
Many to Many Polymorphic Association
Posts
pK: id
title Taggables Tags
Movie body tag_id pk: id
pK: id taggable_id name
title
taggable_type
released
$movie->tags; $post->tags;
Underneath Eloquent Model
Query Builder
Illuminate\Database\Query\Builder
DB::table(‘users’)->get();
DB::table(‘users’)->where(‘email’, ‘[email protected]’)->first();
DB::table(‘users’)
->join(‘contacts’, function($join) {
Wolf
Multiple Routing Paradigms
1. route to closures
2. route to controller actions
3. route to RESTful controllers
4. route to resource
Routing to Closure
Route::get(‘techtalks’, function() {
return “<h1> Welcome </h1>“;
});
Route::post(‘techtalks’, function() {
});
Route::put(‘techtalks/{id}’, function($id) {
});
Route::delete(‘techtalks/{id}’, function($id) {
});
Route::any(‘techtalks’, function() {
});
Route Groups and Filters
Route::get(‘users’, function() {
// get a post by id
});
});
Route::filter(‘auth’, function() {
Route::get(‘/’, function($account) {
Route::get(‘/‘, ‘HomePageController@home’);
Routing to RESTful Controller
class TechTalksController extends Controller {
Route::controller(‘talks’, ‘TechTalksController’);
Routing to Resource Controller
class PostsController extends Controller {
Route::resource(‘posts’, ‘PostsController’);
Other Cool Features
Authentication
$credentials = [‘username’=> ‘raftalks’, ‘password’ => ‘secret’];
Lang File
FILE PATH:
/app
return array(
“world_news” => “ނުދ ޭ ބަޚ ެގ
ި ޔ ަ ރ
ު ”, /lang
“news” => “ބަޚ
ަ ރ
ު ”, /dv
… news.php
Artisan CLI
Laravel Provides
Artisan Commands for
Rapid Development
User::create([
‘username’=>’admin’,
‘password’=> Hash::make(‘password’)
]);
}
}
Events
Event::fire(‘news.created’, $post);
Event::listen(‘news.created’,function($post) {
UserPostCounter::increment(‘posts’, $post->user_id);
});
Mail
Mail::send(‘emails.welcome’, $data, function($message) use($user)
{
$message->to($user->email, $user->name)
->subject(‘Welcome’);
});
SSH::run([
‘cd /var/www’,
‘git pull origin master’
]);
Laravel Homestead
ng Vagrant, we now have easy way to simply manage virtual mac
Included Softwares
• Ubuntu 14.04
• PHP 5.5
• Nginx
• MySQL
• Postgres
• Node (with Bower, Grunt, Gulp)
• Redis
• Memcached
• Beanstalkd
• Laravel Envoy
• Fabric + HipChat Extension
Sign Up -> Add Your Cloud’s API Key -> Serve Happiness
forge.laravel.com
Meet the community @
IRC
Channel: #laravel
Server: irc.freenode.net
Port: 6667
Next Laracon
laracon.eu/2014
Laravel
Maldives Group
Wouldn’t it be awesome for us to have our own Laracons