FABRIC TOKYO での Laravel 開発アーキテクチャ(Laravel/Vue.js勉強会#11)/ laravue-laravel-clean-architecture

FABRIC TOKYO での Laravel 開発アーキテクチャ(Laravel/Vue.js勉強会#11)/ laravue-laravel-clean-architecture

FABRIC TOKYO での Laravel 開発アーキテクチャ 無理しない DDD と Clean Architecture での開発手法(Laravel/Vue.js勉強会#11)

Ada4a6e1e74505a5554861e928622294?s=128

SHUNSUKE Takahashi

October 28, 2019
Tweet

Transcript

  1. FABRIC TOKYOͰͷ
 Laravel ։ൃΞʔΩςΫνϟ ແཧ͠ͳ͍%%%ͱ$MFBO"SDIJUFDUVSFͰͷ։ൃख๏

  2. ୭ ? ໊લߴڮढ़ี λΧϋγγϡϯεέ  -BSBWFMྺ೥ ʙ  '"#3*$50:,0*ODΤϯδχΞ ελʔϑΣεςΟόϧגࣜձࣾΤϯδχΞ

    1)1$POGFSFODF+"1"/࣮ߦҕһ ෭ҕһ௕  1)1FS,BJHJ࣮ߦҕһ
  3. ࿩͞ͳ͍ࣄ w $SFBO"SDIJUFDUVSFͷৄ͍͠಺༰ w "QQMJDBUJPO4FSWJDF 1SFTFOUFSFUDʜ w 7VFKTͷ಺༰

  4. Agenda w '"#3*$50,:0&$ͷٕज़ʹ͍ͭͯ w ͳͥɺ$MFBO"SDIJUFDUVSFͳͷ͔  w ࣮ࡍͷར༻ํ๏ w ར༻ͯ͠ྑ͘ͳͬͨ͜ͱ

  5. FABRIC TOKYO ECͷٕज़ελοΫ w "NB[PO8FC4FSWJDF &$3%44$MPVE'SPOU  w #"$,&/% w

    1)1-BSBWFM YʹόʔδϣϯΞοϓத  w '30/5&/% w 7VFKT K2VFSZ3FBDU
  6. ݹ͖Α͖-".1؀ڥ

  7. FABRIC TOKYO / Architecture

  8. Clean Architecture??

  9. Laravel (MVC) ։ൃͰͷ೰Έ

  10. Α͋͘Δ೰Έ w େྔͷॲཧ͕͋Δ.PEFM w େ͖͗͢Δ$POUSPMMFS w ϏδωεϩδοΫΛ࣋ͬͨ7JFX 5FNQMBUF  w

    ॻ͖ʹ͍͘5FTU
  11. None
  12. Clean Architecture

  13. None
  14. The Clean Architecture

  15. Α͘෼͔Βͳ͍೉ͦ͠͏

  16. ೉͍͠

  17. શͯΛཧղ͠Α͏ͱ͢Δ͔ΒͰ͸ʁ

  18. શͯΛऔΓೖΕͳ͚Ε͹Α͍ͷͰ͸ʁ

  19. Α͋͘Δ೰Έ w େྔͷॲཧ͕͋Δ.PEFM w େ͖͗͢Δ$POUSPMMFS w ϏδωεϩδοΫΛ࣋ͬͨ7JFX 5FNQMBUF  w

    ॻ͖ʹ͍͘5FTU
  20. ஔ͖׵͑ • Model -> Entity (& Repository) • Controller ->

    UseCase • View -> Presenter
  21. Entity

  22. Entity l࠷ॏཁͷϏδωεϧʔϧͱ࠷ॏཁͷϏδωεσʔλ͸ີ઀ʹ݁ͼ෇͍ ͍ͯΔͨΊɺΦϒδΣΫτͷ༗ྗͳީิʹͳΔɻ͜͏ͨ͠ΦϒδΣΫ τͷ͜ͱΛΤϯςΟςΟͱݺͼ͍ͨɻz ʮ$MFBO"SDIJUFDUVSFୡਓʹֶͿιϑτ΢ΣΞͷߏ଄ͱઃܭʯ

  23. UseCase

  24. UseCase lΞϓϦέʔγϣϯݻ༗ͷϏδωεϧʔϧؚ͕·Ε͍ͯΔɻ͜͜ʹ͸ɺγεςϜ ͷ͢΂ͯͷϢʔεέʔε͕ΧϓηϧԽɾ࣮૷͞Ε͍ͯΔɻϢʔεέʔε͸ɺΤϯ ςΟςΟʹೖग़ྗ͢ΔσʔλͷྲྀΕΛௐ੔͠ɺϢʔεέʔεͷ໨ඪΛୡ੒Ͱ͖Δ Α͏ʹɺΤϯςΟςΟʹ࠷ॏཁϏδωεϧʔϧΛ࢖༻͢ΔΑ͏ʹࢦࣔΛग़͢ɻz ʮ$MFBO"SDIJUFDUVSFୡਓʹֶͿιϑτ΢ΣΞͷߏ଄ͱઃܭʯ

  25. ͭ·Γʁ • Entity (Model) ʹ͸ΞϓϦέʔγϣϯ౎߹ͷॲཧΛೖΕͳ͍ɻ • ΞϓϦέʔγϣϯ౎߹ͷॲཧ͸ UseCase Ͱߦ͏΂͖Ͱ͋Δɻ

  26. ྫ୊1045͞ΕͨϢʔβʔ৘ใΛ%#ʹอଘ͢Δ

  27. ಺༰ w OBNF FNBJM QBTTXPSEΛೖྗʢईͷ౎߹্7BMJEBUJPO͸আ͘ʣ w QBTTXPSE͸҉߸Խ

  28. Կ΋ߟ͑ͣʹॻ͘ͱ…

  29. 1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request;

    6 use Illuminate\Support\Facades\Hash; 7 use App\User; 8 9 /** 10 * Class UsersController. 11 * 12 * @package namespace App\Http\Controllers; 13 */ 14 class UsersController extends Controller 15 { 16 public function save(Request $request, $id = null) 17 { 18 $data = $request->all(); 19 20 if ($request->has('password')) { 21 $data['password'] = Hash::make($request->get('password')); 22 } 23 24 return response()->json([ 25 'data' => User::createOrSave([ 26 'id' => $id, 27 ], $data) 28 ]); 29 } 30 }
  30. 1 <?php 2 3 namespace App; 4 5 use Illuminate\Database\Eloquent\Model;

    6 use Illuminate\Support\Facades\Hash; 7 8 class User extends Model 9 { 10 public function save(array $data): User 11 { 12 if (isset($data['password'])) { 13 $data['password'] = Hash::make($data['password']); 14 } 15 16 return $this->createOrSave([ 17 'id' => $data['id'] 18 ], $data); 19 } 20 }
  31. ࠓ͸໰୊ͳͯ͘΋… w ޙʹॲཧ͕૿͑Δࣄ͕૝ఆ͞ΕΔ w ҉߸Խ͢Δॲཧͱอଘ͢Δॲཧ͋Δ w ςετͮ͠Β͍

  32. Let’s Try.
 
 https://github.com/stakahashi/example-laravel-clean-architecture

  33. UsersController

  34. 1 <?php 2 3 namespace App\Http\Controllers; 4 5 use Illuminate\Http\Request;

    6 use App\UseCases\User\SaveUser; 7 8 /** 9 * Class UsersController. 10 * 11 * @package namespace App\Http\Controllers; 12 */ 13 class UsersController extends Controller 14 { 15 public function save(Request $request, SaveUser $useCase, $id = null) 16 { 17 return response()->json([ 18 'data' => $useCase($id, $request->all()) 19 ]); 20 } 21 }
  35. UseCase

  36. 1 <?php 2 namespace App\UseCases\User; 3 4 use App\Entities\User; 5

    use App\Repositories\UserRepositoryInterface; 6 use Illuminate\Support\Arr; 7 use Illuminate\Support\Facades\Hash; 8 9 class SaveUser 10 { 11 /** 12 * @var UserRepositoryInterface 13 */ 14 protected $repository; 15 16 /** 17 * SaveUser constructor. 18 * 19 * @param UserRepositoryInterface $repository 20 */ 21 public function __construct(UserRepositoryInterface $repository) 22 { 23 $this->repository = $repository; 24 } 25 26 public function __invoke($id = null, array $data): User 27 { 28 if (Arr::has($data, 'password')) { 29 $data['password'] = Hash::make($data['password']); 30 } 31 32 return $this->repository->save($id, $data); 33 } 34 }
  37. UserRepository

  38. 1 <?php 2 3 namespace App\Repositories; 4 5 use Illuminate\Database\Eloquent\Collection;

    6 use App\Entities\User; 7 8 /** 9 * Class UserRepository. 10 * 11 * @package namespace App\Repositories; 12 */ 13 class UserRepository implements UserRepositoryInterface 14 { 15 16 public function save($id = null, array $data): User 17 { 18 return User::updateOrCreate([ 19 'id' => $id 20 ], $data); 21 } 22 } 1 <?php 2 3 namespace App\Repositories; 4 5 use Illuminate\Database\Eloquent\Collection; 6 use App\Entities\User; 7 8 /** 9 * Class UserRepository. 10 * 11 * @package namespace App\Repositories; 12 */ 13 class UserRepository implements UserRepositoryInterface 14 { 15 16 public function save($id = null, array $data): User 17 { 18 return User::updateOrCreate([ 19 'id' => $id 20 ], $data); 21 } 22 }
  39. UserEntity

  40. 1 <?php 2 3 namespace App\Entities; 4 5 use Illuminate\Database\Eloquent\Model;

    6 7 /** 8 * Class User. 9 * 10 * @package namespace App\Entities; 11 */ 12 class User extends Model 13 { 14 15 /** 16 * The attributes that are mass assignable. 17 * 18 * @var array 19 */ 20 protected $guarded = ['id']; 21 22 }
  41. ֤߲໨ͷ໾ׂ w $POUSPMMFS w ϦΫΤετ͔ΒσʔλΛ6TF$BTFʹ౉͠ɺϨεϙϯεΛฦ͢ w 6TF$BTF w σʔλΛड͚औͬͯՃ޻ʢQBTTXPSEͷ)BTIԽʣΛߦ͍ɺ3FQPTJUPSZ΁౉͢ w

    3FQPTJUPSZ w %#ʹड͚औͬͨσʔλΛॻ͖ࠐΉ w &OUJUZ w 6TFSࣗମͷ࣮ମ
  42. Կ͕ྑ͘ͳͬͨͷ͔ʁ

  43. ྑ͘ͳͬͨ͜ͱ w $POUSPMMFS w ॲཧ͸6TF$BTFʹ͋ΔͷͰബ͘ͳΔɻ w ϦΫΤετͱϨεϙϯεΛؾʹ͢Ε͹Α͍ɻ w .PEFM &OUJUZ

     w ΞϓϦέʔγϣϯʹґଘ͢Δॲཧ͸6TF$BTF͕ߦ͏ͷͰঢ়ଶͱͯ͠ѻ͍΍͍͢ w %#΁ͷॲཧ͸3FQPTJUPSZΛհͯ͠ߦ͏ͷͰॲཧࣗମ͸3FQPTJUPSZʹ৐Γɺ.PEFM͕ബ͘ͳΔɻ w 5FTU w ֤ॲཧ͕খ͘͞͠΍͘͢ͳΔͷͰ୯ମͰͷςετ͕༰қʹͳΔ
  44. ࠷ޙʹ

  45. ࠷ޙʹ w -BSBWFMΛී௨ʹॻ͘ͱະདྷۤ͘͠ͳΓ͕ͪͳͷͰ
 %%%΍$MFBO"SDIJUFDUVSF͸ଉͷ௕͍ΞϓϦέʔγϣϯΛ։ ൃ͢ΔࡍͷΞϓϩʔνͱͯ͠͸ݕ౼ͨ͠΄͏͕͍͍ͱࢥ͏ɻ w $MFBO"SDIJUFDUVSFͷશͯΛऔΓೖΕͣͱ΋෦෼తʹ࢖༻͢Δ ͜ͱͰԸܙ͸ड͚ΒΕΔɻ w νʔϜ։ൃͰͷ$MFBO"SDIJUFDUVSFͷಋೖʹϋʔυϧΛ


    Լ͛ͯελʔτ͢Δ͜ͱ΋Ͱ͖Δɻ
  46. ແཧͳ͘%%%$MFBO"SDIJUFDUVSFʹ
 ৮ΕͯΈΑ͏

  47. ͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ