Laravel 5 In Depth
-
Upload
kirk-bushell -
Category
Software
-
view
2.185 -
download
6
Transcript of Laravel 5 In Depth
![Page 1: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/1.jpg)
LARAVEL 5IN DEPTH
Kirk Bushell
![Page 2: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/2.jpg)
LARAVEL 5IN DEPTH
(sort of)
Kirk Bushell
![Page 3: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/4.jpg)
CAVEATS
![Page 5: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/5.jpg)
● Laravel 5 in 30 mins? Yeah… no.
CAVEATS
![Page 6: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/6.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/7.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/8.jpg)
LET’S GET STARTED
![Page 9: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/9.jpg)
Route::get(‘user/profile’, ‘MemberController@profile’);Route::put(‘user/profile’, ‘MemberController@saveProfile’);
![Page 10: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/10.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/14.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/15.jpg)
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
![Page 16: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/16.jpg)
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
![Page 17: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/17.jpg)
class SaveProfileRequest extends FormRequest{
public function authorize() {return true;
}
public function rules() {return [
‘username’ => [‘required’],‘email’ => [‘required’, ‘email’]
];}
}
![Page 18: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/19.jpg)
class SaveProfileCommand{
public $username;public $email;
public function __construct($username, $email) {$this->username = $username;$this->email = $email;
}}
![Page 20: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/20.jpg)
class SaveProfileCommandHandler{
private $users;
public function __construct(UserRepositoryInterface $users) {$this->users = $users;
}
public function handle(SaveProfileCommand $command) {...
}}
![Page 21: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/23.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/24.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/25.jpg)
class MemberHasUpdatedProfile{
public $user;
public function __construct(User $user) {$this->user = $user;
}}
![Page 26: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/26.jpg)
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
class ServiceProvider extends EventServiceProvider{
protected $listen = [‘MemberHasUpdatedProfile’ => [
‘MemberListener@whenMemberHasUpdatedProfile’]
];}
![Page 27: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/28.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/29.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/30.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/31.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/32.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/33.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/34.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/35.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/36.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/39.jpg)
ERMAHGERD! SOMEONE UPDATED
MAH PROFILEZ!!!
![Page 40: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/40.jpg)
SELF-HANDLING COMMANDS
![Page 41: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/41.jpg)
● Scared by all the code?
SELF-HANDLING COMMANDS
![Page 42: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/42.jpg)
● Scared by all the code?
● Don’t worry - Laravel has you covered
SELF-HANDLING COMMANDS
![Page 43: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/43.jpg)
● Scared by all the code?
● Don’t worry - Laravel has you covered ;)
SELF-HANDLING COMMANDS
![Page 44: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/44.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/45.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/47.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/48.jpg)
class SaveProfileCommand implements SelfHandling{
...
public function handle() {$user = Auth::user();$user->username = $this->username;$user->email = $this->email;
...}
}
![Page 49: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/49.jpg)
RECAP
![Page 50: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/50.jpg)
● Laravel’s command architecture rocks
RECAP
![Page 51: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/51.jpg)
● Laravel’s command architecture rocks
● Helps us to easily isolate specific use-cases
RECAP
![Page 52: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/52.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/53.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/54.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/55.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/56.jpg)
● 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](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/57.jpg)
THANK YOU :)
![Page 58: Laravel 5 In Depth](https://reader036.fdocuments.in/reader036/viewer/2022062419/55a522301a28abbf348b4842/html5/thumbnails/58.jpg)
THANK YOU :)
● http://kirkbushell.me
● https://github.com/kirkbushell
● @kirkbushell