Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
 @shin1x1
2015/04/20 21th Kansai PHP Study
How to learn
Laravel 5 application
from Authentication
Laravel book
(c) 2015 Masashi Shinbara @shin1x1
• First Japanese paper book
• for Laravel 4.2 

(include L5 new features)
...
Larval 5
Authentication
Authentication
(c) 2015 Masashi Shinbara @shin1x1
• Include default project
• Routing, Controller, Service, Model,

Migrat...
Register
(c) 2015 Masashi Shinbara @shin1x1
Login
(c) 2015 Masashi Shinbara @shin1x1
Reset Password
(c) 2015 Masashi Shinbara @shin1x1
Other features
(c) 2015 Masashi Shinbara @shin1x1
• Remember me
• Basic Authentication
• Social Authentication

(twitter, ...
Architecture
of
Autheincation
Layerd architecture
(c) 2015 Masashi Shinbara @shin1x1
Routing
(c) 2015 Masashi Shinbara @shin1x1
Routing
Route::controllers([

'auth' => 'AuthAuthController',

'password' => 'AuthPasswordController',

]);
Implicit Contr...
Controller Method
(c) 2015 Masashi Shinbara @shin1x1
• method = HTTP method + /uri/path
• getRegister() = GET /auth/regist...
Routing - auth
(c) 2015 Masashi Shinbara @shin1x1
Method URI Action
GET¦HEAD /auth/register AuthController@getRegister
POS...
Routing - password
(c) 2015 Masashi Shinbara @shin1x1
Method URI Action
GET¦HEAD /password/email PasswordController@getEma...
Controller
(c) 2015 Masashi Shinbara @shin1x1
Controller
(c) 2015 Masashi Shinbara @shin1x1
• AuthController / PasswordController
• Have a constructor only
• Use traits...
Controller
<?php namespace AppHttpControllersAuth;



// (snip)



class AuthController extends Controller

{

use Authent...
Controller
<?php namespace AppHttpControllersAuth;



// (snip)



class AuthController extends Controller

{

use Authent...
Controller
<?php namespace AppHttpControllersAuth;



// (snip)



class AuthController extends Controller

{

use Authent...
Constructor Injection
(c) 2015 Masashi Shinbara @shin1x1
• Use type hinting in constructor
• Inject instances from IoC Con...
Constructor Injection
public function __construct(Guard $auth, Registrar $registrar)

{

$this->auth = $auth;

$this->regi...
Middleware
(c) 2015 Masashi Shinbara @shin1x1
• Decorator pattern
• Implement Middleware Contracts
• Middleware call next ...
Middleware
public function handle($request, Closure $next)

{

if ($this->auth->check()) {

return new RedirectResponse(ur...
Middleware Chain
CheckForMaintenanceMode
EncryptCookies
AddQueuedCookiesToResponse
StartSession
ShareErrorsForSession
Redi...
Trait
(c) 2015 Masashi Shinbara @shin1x1
• AuthenticatesAndRegistersUsers
• ResetsPasswords
• Implemented controller logic
Trait
public function getRegister()

{

return view('auth.register');

}



public function postRegister(Request $request)...
Trait
public function getRegister()

{

return view('auth.register');

}



public function postRegister(Request $request)...
Service
(c) 2015 Masashi Shinbara @shin1x1
Service
(c) 2015 Masashi Shinbara @shin1x1
• Called by controller(with trait)
• Implement business logic
• Delegate data p...
Service - Registrar
class Registrar implements RegistrarContracts

{

public function validator(array $data)

{

return Va...
Service - Registrar
class Registrar implements RegistrarContracts

{

public function validator(array $data)

{

return Va...
Controller(with Trait)
public function postRegister(Request $request)

{

$validator = $this->registrar->validator($reques...
Model(Eloquent)
(c) 2015 Masashi Shinbara @shin1x1
Model(Eloquent)
(c) 2015 Masashi Shinbara @shin1x1
• Implement Contracts(interface)
• Use some traits
• Only define property
Model(Eloquent)
class User extends Model implements AuthenticatableContracts,
CanResetPasswordContracts

{

use Authentica...
Conclusion
Conclusion
(c) 2015 Masashi Shinbara @shin1x1
• Authentication is a good sample

for L5 application
• Separate layers and
...
@shin1x1
(c) 2015 Masashi Shinbara @shin1x1
Upcoming SlideShare
Loading in …5
×

How to learn Laravel5 application from Authentication

1,738 views

Published on

Published in: Technology
  • Use Laravel Passport for API authentication (https://www.cloudways.com/blog/rest-api-laravel-passport-authentication/ ). JWT and JSON are old and less efficient method of doing this. Passport makes the process a lot easier and there are less chances that you will experience any issue in it.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

How to learn Laravel5 application from Authentication

  1. 1.  @shin1x1 2015/04/20 21th Kansai PHP Study How to learn Laravel 5 application from Authentication
  2. 2. Laravel book (c) 2015 Masashi Shinbara @shin1x1 • First Japanese paper book • for Laravel 4.2 
 (include L5 new features) • I wrote Chapter5
 (REST API application) http://www.amazon.co.jp/dp/4774173134/
  3. 3. Larval 5 Authentication
  4. 4. Authentication (c) 2015 Masashi Shinbara @shin1x1 • Include default project • Routing, Controller, Service, Model,
 Migration File • Sample L5 Application
  5. 5. Register (c) 2015 Masashi Shinbara @shin1x1
  6. 6. Login (c) 2015 Masashi Shinbara @shin1x1
  7. 7. Reset Password (c) 2015 Masashi Shinbara @shin1x1
  8. 8. Other features (c) 2015 Masashi Shinbara @shin1x1 • Remember me • Basic Authentication • Social Authentication
 (twitter, github, etc)
  9. 9. Architecture of Autheincation
  10. 10. Layerd architecture (c) 2015 Masashi Shinbara @shin1x1
  11. 11. Routing (c) 2015 Masashi Shinbara @shin1x1
  12. 12. Routing Route::controllers([
 'auth' => 'AuthAuthController',
 'password' => 'AuthPasswordController',
 ]); Implicit Controllers
  13. 13. Controller Method (c) 2015 Masashi Shinbara @shin1x1 • method = HTTP method + /uri/path • getRegister() = GET /auth/register • postRegister() = POST /auth/register
  14. 14. Routing - auth (c) 2015 Masashi Shinbara @shin1x1 Method URI Action GET¦HEAD /auth/register AuthController@getRegister POST /auth/register AuthController@postRegister GET¦HEAD /auth/login AuthController@getLogin POST /auth/login AuthController@postLogin GET¦HEAD /auth/logout AuthController@getLogout
  15. 15. Routing - password (c) 2015 Masashi Shinbara @shin1x1 Method URI Action GET¦HEAD /password/email PasswordController@getEmail POST /password/email PasswordController@postEmail GET¦HEAD /password/reset PasswordController@getReset POST /password/reset PasswordController@postReset
  16. 16. Controller (c) 2015 Masashi Shinbara @shin1x1
  17. 17. Controller (c) 2015 Masashi Shinbara @shin1x1 • AuthController / PasswordController • Have a constructor only • Use traits
 (action methods are implemented)
  18. 18. Controller <?php namespace AppHttpControllersAuth;
 
 // (snip)
 
 class AuthController extends Controller
 {
 use AuthenticatesAndRegistersUsers;
 
 public function __construct(Guard $auth, Registrar $registrar)
 {
 $this->auth = $auth;
 $this->registrar = $registrar;
 
 $this->middleware('guest', ['except' => 'getLogout']);
 }
 
 } Constructor Injection
  19. 19. Controller <?php namespace AppHttpControllersAuth;
 
 // (snip)
 
 class AuthController extends Controller
 {
 use AuthenticatesAndRegistersUsers;
 
 public function __construct(Guard $auth, Registrar $registrar)
 {
 $this->auth = $auth;
 $this->registrar = $registrar;
 
 $this->middleware('guest', ['except' => 'getLogout']);
 }
 
 } middleware
 (Redirect if authenticated)
  20. 20. Controller <?php namespace AppHttpControllersAuth;
 
 // (snip)
 
 class AuthController extends Controller
 {
 use AuthenticatesAndRegistersUsers;
 
 public function __construct(Guard $auth, Registrar $registrar)
 {
 $this->auth = $auth;
 $this->registrar = $registrar;
 
 $this->middleware('guest', ['except' => 'getLogout']);
 }
 
 } trait
  21. 21. Constructor Injection (c) 2015 Masashi Shinbara @shin1x1 • Use type hinting in constructor • Inject instances from IoC Container • Container build a new instance
 if container don t have it
  22. 22. Constructor Injection public function __construct(Guard $auth, Registrar $registrar)
 {
 $this->auth = $auth;
 $this->registrar = $registrar;
 
 $this->middleware('guest', ['except' => 'getLogout']);
 } $auth = Guard class instance $registar = Registar class instance
  23. 23. Middleware (c) 2015 Masashi Shinbara @shin1x1 • Decorator pattern • Implement Middleware Contracts • Middleware call next middleware • Single Responsibility
  24. 24. Middleware public function handle($request, Closure $next)
 {
 if ($this->auth->check()) {
 return new RedirectResponse(url('/home'));
 }
 
 return $next($request);
 } Redirect to /home if logined otherwise call next middleware
  25. 25. Middleware Chain CheckForMaintenanceMode EncryptCookies AddQueuedCookiesToResponse StartSession ShareErrorsForSession RedirectIfAuthenticate Controller method
  26. 26. Trait (c) 2015 Masashi Shinbara @shin1x1 • AuthenticatesAndRegistersUsers • ResetsPasswords • Implemented controller logic
  27. 27. Trait public function getRegister()
 {
 return view('auth.register');
 }
 
 public function postRegister(Request $request)
 {
 $validator = $this->registrar->validator($request->all());
 
 if ($validator->fails())
 {
 $this->throwValidationException(
 $request, $validator
 );
 }
 
 $this->auth->login($this->registrar->create($request->all()));
 
 return redirect($this->redirectPath());
 } method
  28. 28. Trait public function getRegister()
 {
 return view('auth.register');
 }
 
 public function postRegister(Request $request)
 {
 $validator = $this->registrar->validator($request->all());
 
 if ($validator->fails())
 {
 $this->throwValidationException(
 $request, $validator
 );
 }
 
 $this->auth->login($this->registrar->create($request->all()));
 
 return redirect($this->redirectPath());
 } method injection
  29. 29. Service (c) 2015 Masashi Shinbara @shin1x1
  30. 30. Service (c) 2015 Masashi Shinbara @shin1x1 • Called by controller(with trait) • Implement business logic • Delegate data persistence to Eloquent • Guard class / Registrar class
  31. 31. Service - Registrar class Registrar implements RegistrarContracts
 {
 public function validator(array $data)
 {
 return Validator::make($data, [
 'name' => 'required|max:255',
 'email' => 'required|email|max:255|unique:users',
 'password' => 'required|confirmed|min:6',
 ]);
 }
 
 public function create(array $data)
 {
 return User::create([
 'name' => $data['name'],
 'email' => $data['email'],
 'password' => bcrypt($data['password']),
 ]);
 }
 } validator builder
  32. 32. Service - Registrar class Registrar implements RegistrarContracts
 {
 public function validator(array $data)
 {
 return Validator::make($data, [
 'name' => 'required|max:255',
 'email' => 'required|email|max:255|unique:users',
 'password' => 'required|confirmed|min:6',
 ]);
 }
 
 public function create(array $data)
 {
 return User::create([
 'name' => $data['name'],
 'email' => $data['email'],
 'password' => bcrypt($data['password']),
 ]);
 }
 } business logic
  33. 33. Controller(with Trait) public function postRegister(Request $request)
 {
 $validator = $this->registrar->validator($request->all());
 
 if ($validator->fails())
 {
 $this->throwValidationException(
 $request, $validator
 );
 }
 
 $this->auth->login($this->registrar->create($request->all()));
 
 return redirect($this->redirectPath());
 } call service call servicecall service
  34. 34. Model(Eloquent) (c) 2015 Masashi Shinbara @shin1x1
  35. 35. Model(Eloquent) (c) 2015 Masashi Shinbara @shin1x1 • Implement Contracts(interface) • Use some traits • Only define property
  36. 36. Model(Eloquent) class User extends Model implements AuthenticatableContracts, CanResetPasswordContracts
 {
 use Authenticatable, CanResetPassword;
 protected $table = 'users';
 protected $fillable = ['name', 'email', 'password'];
 protected $hidden = ['password', 'remember_token'];
 } Contracts(interface) Trait
  37. 37. Conclusion
  38. 38. Conclusion (c) 2015 Masashi Shinbara @shin1x1 • Authentication is a good sample
 for L5 application • Separate layers and
 make a clear role in each layers • Unix philosophy
  39. 39. @shin1x1 (c) 2015 Masashi Shinbara @shin1x1

×