laravel 5 in depth
Post on 14-Jul-2015
2.193 Views
Preview:
TRANSCRIPT
LARAVEL 5IN DEPTH
Kirk Bushell
LARAVEL 5IN DEPTH
(sort of)
Kirk Bushell
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
CAVEATS
● Laravel 5 in 30 mins? Yeah… no.
CAVEATS
● 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
● 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
LET’S GET STARTED
Route::get(‘user/profile’, ‘MemberController@profile’);Route::put(‘user/profile’, ‘MemberController@saveProfile’);
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’);}
}
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’);}
}
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’);}
}
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’);}
}
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’);}
}
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
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’);}
}
class SaveProfileCommand{
public $username;public $email;
public function __construct($username, $email) {$this->username = $username;$this->email = $email;
}}
class SaveProfileCommandHandler{
private $users;
public function __construct(UserRepositoryInterface $users) {$this->users = $users;
}
public function handle(SaveProfileCommand $command) {...
}}
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));}
}
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));}
}
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));}
}
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));}
}
class MemberHasUpdatedProfile{
public $user;
public function __construct(User $user) {$this->user = $user;
}}
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
class ServiceProvider extends EventServiceProvider{
protected $listen = [‘MemberHasUpdatedProfile’ => [
‘MemberListener@whenMemberHasUpdatedProfile’]
];}
class MemberListener{
use DispatchesCommands;
public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(
new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’
));
}}
class MemberListener{
use DispatchesCommands;
public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(
new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’
));
}}
class MemberListener{
use DispatchesCommands;
public function whenMemberHasUpdatedProfile(User $member) {$this->dispatch(
new SendNotificationCommand($member, ‘profile.notification’,$member->email,‘Profile updated’
));
}}
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;
}}
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;
}}
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;
}}
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;
}}
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);}
}
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);}
}
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);}
}
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);}
}
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);}
}
ERMAHGERD! SOMEONE UPDATED
MAH PROFILEZ!!!
SELF-HANDLING COMMANDS
● Scared by all the code?
SELF-HANDLING COMMANDS
● Scared by all the code?
● Don’t worry - Laravel has you covered
SELF-HANDLING COMMANDS
● Scared by all the code?
● Don’t worry - Laravel has you covered ;)
SELF-HANDLING COMMANDS
● Scared by all the code?
● Don’t worry - Laravel has you covered ;)
● Possible to have commands handle themselves
SELF-HANDLING COMMANDS
● Scared by all the code?
● Don’t worry - Laravel has you covered ;)
● Possible to have commands handle themselves
● How?
SELF-HANDLING COMMANDS
class SaveProfileCommand implements SelfHandling{
public $username;public $email;
public function __construct($username, $email) {$this->username = $username;$this->email = $email;
}
public function handle() {...
}}
class SaveProfileCommand implements SelfHandling{
public $username;public $email;
public function __construct($username, $email) {$this->username = $username;$this->email = $email;
}
public function handle() {...
}}
class SaveProfileCommand implements SelfHandling{
...
public function handle() {$user = Auth::user();$user->username = $this->username;$user->email = $this->email;
...}
}
RECAP
● Laravel’s command architecture rocks
RECAP
● Laravel’s command architecture rocks
● Helps us to easily isolate specific use-cases
RECAP
● Laravel’s command architecture rocks
● Helps us to easily isolate specific use-cases
● We can queue expensive or time-consuming tasks
RECAP
● 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
● 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
● 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
● 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
THANK YOU :)
THANK YOU :)
● http://kirkbushell.me
● https://github.com/kirkbushell
● @kirkbushell
top related