58
LARAVEL 5 IN DEPTH Kirk Bushell

Laravel 5 In Depth

Embed Size (px)

Citation preview

Page 1: Laravel 5 In Depth

LARAVEL 5IN DEPTH

Kirk Bushell

Page 2: Laravel 5 In Depth

LARAVEL 5IN DEPTH

(sort of)

Kirk Bushell

Page 3: Laravel 5 In Depth

INTRODUCTION

● Developer - 15 years experience

● Technical lead - Tectonic Digital

● Software architect - Award Force - http://awardforce.com

● Information Technologies Coordinator - Engineers without Borders

● Technical writer - http://kirkbushell.me

● Twitter - @kirkbushell

● Github - https://github.com/kirkbushell

Page 4: Laravel 5 In Depth

CAVEATS

Page 5: Laravel 5 In Depth

● Laravel 5 in 30 mins? Yeah… no.

CAVEATS

Page 6: Laravel 5 In Depth

● Laravel 5 in 30 mins? Yeah… no.

● A whirlwind tour of Laravel 5 features, plus a few opinionated bits on

good application structure and design.

CAVEATS

Page 7: Laravel 5 In Depth

● Laravel 5 in 30 mins? Yeah… no.

● A whirlwind tour of Laravel 5 features, plus a few opinionated bits on

good application structure and design.

● We’ll go over a user (member?) profile management use-case, with some

perhaps ridiculous requirements.

CAVEATS

Page 8: Laravel 5 In Depth

LET’S GET STARTED

Page 9: Laravel 5 In Depth

Route::get(‘user/profile’, ‘MemberController@profile’);Route::put(‘user/profile’, ‘MemberController@saveProfile’);

Page 10: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘member.profile’, [‘member’ => Auth::user()]);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 11: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘member.profile’, [‘member’ => Auth::user()]);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 12: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘member.profile’, [‘member’ => Auth::user()]);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 13: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘member.profile’, [‘member’ => Auth::user()]);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 14: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘user.profile’);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 15: Laravel 5 In Depth

class SaveProfileRequest extends FormRequest{

public function authorize() {return true;

}

public function rules() {return [

‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]

];}

}

Page 16: Laravel 5 In Depth

class SaveProfileRequest extends FormRequest{

public function authorize() {return true;

}

public function rules() {return [

‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]

];}

}

Page 17: Laravel 5 In Depth

class SaveProfileRequest extends FormRequest{

public function authorize() {return true;

}

public function rules() {return [

‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]

];}

}

Page 18: Laravel 5 In Depth

class MemberController{

use DispatchesCommands;

public function profile() {return view(‘user.profile’);

}

public function saveProfile(SaveProfileRequest $request) {$this->dispatchFrom(SaveProfileCommand::class, $request);

return redirect()->route(‘member.profile’);}

}

Page 19: Laravel 5 In Depth

class SaveProfileCommand{

public $username;public $email;

public function __construct($username, $email) {$this->username = $username;$this->email = $email;

}}

Page 20: Laravel 5 In Depth

class SaveProfileCommandHandler{

private $users;

public function __construct(UserRepositoryInterface $users) {$this->users = $users;

}

public function handle(SaveProfileCommand $command) {...

}}

Page 21: Laravel 5 In Depth

class SaveProfileCommandHandler{

...

public function handle(SaveProfileCommand $command) {$user = Auth::user();$user->username = $command->username;$user->email = $command->email;

$this->users->save($user);

Event::fire(new MemberProfileSaved($user));}

}

Page 22: Laravel 5 In Depth

class SaveProfileCommandHandler{

...

public function handle(SaveProfileCommand $command) {$user = Auth::user();$user->username = $command->username;$user->email = $command->email;

$this->users->save($user);

Event::fire(new MemberProfileSaved($user));}

}

Page 23: Laravel 5 In Depth

class SaveProfileCommandHandler{

...

public function handle(SaveProfileCommand $command) {$user = Auth::user();$user->username = $command->username;$user->email = $command->email;

$this->users->save($user);

Event::fire(new MemberHasUpdatedProfile($user));}

}

Page 24: Laravel 5 In Depth

class SaveProfileCommandHandler{

...

public function handle(SaveProfileCommand $command) {$user = Auth::user();$user->username = $command->username;$user->email = $command->email;

$this->users->save($user);

Event::fire(new MemberHasUpdatedProfile($user));}

}

Page 25: Laravel 5 In Depth

class MemberHasUpdatedProfile{

public $user;

public function __construct(User $user) {$this->user = $user;

}}

Page 26: Laravel 5 In Depth

use Illuminate\Foundation\Support\Providers\EventServiceProvider;

class ServiceProvider extends EventServiceProvider{

protected $listen = [‘MemberHasUpdatedProfile’ => [

‘MemberListener@whenMemberHasUpdatedProfile’]

];}

Page 27: Laravel 5 In Depth

class MemberListener{

use DispatchesCommands;

public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(

new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’

));

}}

Page 28: Laravel 5 In Depth

class MemberListener{

use DispatchesCommands;

public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(

new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’

));

}}

Page 29: Laravel 5 In Depth

class MemberListener{

use DispatchesCommands;

public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(

new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’

));

}}

Page 30: Laravel 5 In Depth

class SendNotificationCommand implements ShouldBeQueued{

use SerializesModels;

public $data;public $template;public $to;public $subject;

public function __construct($data, $template, $to, $subject) {$this->data = $data;$this->template = $template;$this->to = $to;$this->subject = $subject;

}}

Page 31: Laravel 5 In Depth

class SendNotificationCommand implements ShouldBeQueued{

use SerializesModels;

public $data;public $template;public $to;public $subject;

public function __construct($data, $template, $to, $subject) {$this->data = $data;$this->template = $template;$this->to = $to;$this->subject = $subject;

}}

Page 32: Laravel 5 In Depth

class SendNotificationCommand implements ShouldBeQueued{

use SerializesModels;

public $data;public $template;public $to;public $subject;

public function __construct($data, $template, $to, $subject) {$this->data = $data;$this->template = $template;$this->to = $to;$this->subject = $subject;

}}

Page 33: Laravel 5 In Depth

class SendNotificationCommand implements ShouldBeQueued{

use SerializesModels;

public $data;public $template;public $to;public $subject;

public function __construct($data, $template) {$this->data = $data;$this->template = $template;$this->to = $to;$this->subject = $subject;

}}

Page 34: Laravel 5 In Depth

class SendNotificationCommandHandler{

public function handle(SendNotificationCommand $command) {$vars = [‘data’ => $command->data];

$handler = function($message) use ($command) {$message->to($command->to);$message->subject($command->subject);

};

Mail::send($command->template, $vars, $handler);}

}

Page 35: Laravel 5 In Depth

class SendNotificationCommandHandler{

public function handle(SendNotificationCommand $command) {$vars = [‘data’ => $command->data];

$handler = function($message) use ($command) {$message->to($command->to);$message->subject($command->subject);

};

Mail::send($command->template, $vars, $handler);}

}

Page 36: Laravel 5 In Depth

class SendNotificationCommandHandler{

public function handle(SendNotificationCommand $command) {$vars = [‘data’ => $command->data];

$handler = function($message) use ($command) {$message->to($command->to);$message->subject($command->subject);

};

Mail::send($command->template, $vars, $handler);}

}

Page 37: Laravel 5 In Depth

class SendNotificationCommandHandler{

public function handle(SendNotificationCommand $command) {$vars = [‘data’ => $command->data];

$handler = function($message) use ($command) {$message->to($command->to);$message->subject($command->subject);

};

Mail::send($command->template, $vars, $handler);}

}

Page 38: Laravel 5 In Depth

class SendNotificationCommandHandler{

public function handle(SendNotificationCommand $command) {$vars = [‘data’ => $command->data];

$handler = function($message) use ($command) {$message->to($command->to);$message->subject($command->subject);

};

Mail::send($command->template, $vars, $handler);}

}

Page 39: Laravel 5 In Depth

ERMAHGERD! SOMEONE UPDATED

MAH PROFILEZ!!!

Page 40: Laravel 5 In Depth

SELF-HANDLING COMMANDS

Page 41: Laravel 5 In Depth

● Scared by all the code?

SELF-HANDLING COMMANDS

Page 42: Laravel 5 In Depth

● Scared by all the code?

● Don’t worry - Laravel has you covered

SELF-HANDLING COMMANDS

Page 43: Laravel 5 In Depth

● Scared by all the code?

● Don’t worry - Laravel has you covered ;)

SELF-HANDLING COMMANDS

Page 44: Laravel 5 In Depth

● Scared by all the code?

● Don’t worry - Laravel has you covered ;)

● Possible to have commands handle themselves

SELF-HANDLING COMMANDS

Page 45: Laravel 5 In Depth

● Scared by all the code?

● Don’t worry - Laravel has you covered ;)

● Possible to have commands handle themselves

● How?

SELF-HANDLING COMMANDS

Page 46: Laravel 5 In Depth

class SaveProfileCommand implements SelfHandling{

public $username;public $email;

public function __construct($username, $email) {$this->username = $username;$this->email = $email;

}

public function handle() {...

}}

Page 47: Laravel 5 In Depth

class SaveProfileCommand implements SelfHandling{

public $username;public $email;

public function __construct($username, $email) {$this->username = $username;$this->email = $email;

}

public function handle() {...

}}

Page 48: Laravel 5 In Depth

class SaveProfileCommand implements SelfHandling{

...

public function handle() {$user = Auth::user();$user->username = $this->username;$user->email = $this->email;

...}

}

Page 49: Laravel 5 In Depth

RECAP

Page 50: Laravel 5 In Depth

● Laravel’s command architecture rocks

RECAP

Page 51: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

RECAP

Page 52: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

● We can queue expensive or time-consuming tasks

RECAP

Page 53: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

● We can queue expensive or time-consuming tasks

● Commands indicate intention

RECAP

Page 54: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

● We can queue expensive or time-consuming tasks

● Commands indicate intention

● Easy to test, super easy to spot problems

RECAP

Page 55: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

● We can queue expensive or time-consuming tasks

● Commands indicate intention

● Easy to test, super easy to spot problems

● Events allow us to easily implement side-effects (such as email)

RECAP

Page 56: Laravel 5 In Depth

● Laravel’s command architecture rocks

● Helps us to easily isolate specific use-cases

● We can queue expensive or time-consuming tasks

● Commands indicate intention

● Easy to test, super easy to spot problems

● Events allow us to easily implement side-effects (such as email)

● You can be as simple or as complex as you like

RECAP

Page 57: Laravel 5 In Depth

THANK YOU :)

Page 58: Laravel 5 In Depth

THANK YOU :)

● http://kirkbushell.me

● https://github.com/kirkbushell

● @kirkbushell