Upgrade to Pro — share decks privately, control downloads, hide ads and more …

LaravelのFat Controllerをどうリファクタしていくか

LaravelのFat Controllerをどうリファクタしていくか

LaravelのFat Controllerをどうリファクタしていくか

02886e95c50822b9aa2552da1e518491?s=128

Takayuki

August 25, 2021
Tweet

Transcript

  1. LaravelͷFat ControllerΛͲ͏ ϦϑΝΫλ͍͔ͯ͘͠

  2. 3 LaravelͰͷϦϑΝΫλํ਑ Agenda 2 ϓϩδΣΫτͷ໰୊఺ 1 ࣗݾ঺հ 4 ϦϑΝΫλ಺༰

  3. 1 ࣗݾ঺հ

  4. 4 ࣗݾ঺հ ◆໊લ ླ໦ ޹೭(Suzuki Takayuki) ◆ࣗݾ঺հ ɾ1990೥ɺਆಸ઒ݝੜ·Εɻ ɾࣾձਓʹͳ͔ͬͯΒɺϓϩάϥϛϯάΛ࢝ΊΔɻ ɾ2015೥ʹϑϧεϐʔυʹೖࣾ͠ɺ2017೥ʹϑϦʔϥ

    ϯεΛ΍ͬͨޙʹ2017೥11݄ʹԭೄͰىۀɻ ◆झຯ ίεϓϨɺԻָϥΠϒɺϚϥιϯେձʹग़Δɺ໺ٿ؍ ઓ ◆Twitter @kanbo0605
  5. Copyright Re:Build.inc All Rights Reserved. ࣗݾ঺հ 1

  6. Copyright Re:Build.inc All Rights Reserved. ձࣾ঺հ 1 ◆ձ໊ࣾ גࣜձࣾRe:Build ◆ॅॴ

    ԭೄݝಹ೼ࢢٱໜ஍2-2-2 λΠϜεϏϧ ◆ࣄۀ಺༰ ɾࣗࣾWEBαʔϏεͷاըɾ։ൃɻ ɾडୗ։ൃ ◆ࣾһ 10໊ ◆ΤϯδχΞͷಇ͖ํ ϦϞʔτϫʔΫՄೳͳࣗ༝ͳಇ͖ํΛਪ঑ͯ͠·͢ʂ
  7. Copyright Re:Build.inc All Rights Reserved. ࣗݾ঺հ 1

  8. Copyright Re:Build.inc All Rights Reserved. ࣗݾ঺հ 1

  9. Copyright Re:Build.inc All Rights Reserved. 9 ɾαʔϏε໊ Tadoru
 https://tadoru.work/
 


    ɾγεςϜ֓ཁ ΤϯδχΞಛԽܕͷ ϦϑΝϥϧ Ϋϥ΢υιʔγϯά 1 ձࣾ঺հ
  10. Copyright Re:Build.inc All Rights Reserved. 1 0 ɾαʔϏε໊ Re:Build Boot

    Camp
 https://rebuild-bootcamp.jp/
 
 ɾ֓ཁ ԭೄͱٶ࡚Ͱʮ࣮຿ϨϕϧʯΛ໨ ࢦ͢ํ޲͚ͷϓϩάϥϛϯάε Ϋʔϧ ɾ࢖͏ٕज़ LaravelɺVue.jsͳͲ 1 ձࣾ঺հ
  11. Copyright Re:Build.inc All Rights Reserved. 11 ࣮຿Ϩϕϧ ॳ৺ऀ ॳڃऀ ஍ํͷॳ৺ऀ޲͚ͷϓϩάϥ

    ϛϯάεΫʔϧΛଔۀͨ͠ޙ ͷॳڃऀ͔Β࣮຿Ϩϕϧ·Ͱ ͷؒΛຒΊΔεΫʔϧ 1 ձࣾ঺հ
  12. 1 2 ͜ͷൃදͷର৅ऀ ɾLaravelΛ࣮຿Ͱ࢖͍࢝Ίͨਓ ɾϓϩδΣΫτ͕େ͖͘ͳ͖ͬͯͯɺίʔυͷ੔ཧͰ೰ΜͰ͍Δਓ ɾϓϩδΣΫτͰίʔσΟϯάن໿͕ͳͯ͘ࠔ͍ͬͯΔ

  13. 2 ϓϩδΣΫτͷ໰୊఺

  14. 1 4 2 ϓϩδΣΫτͷ໰୊఺ ɾ͓ͦΒ͘ɺJavaग़਎ͷLaravelॳ৺ऀΛ͔͖ूΊ ͨঢ়ଶ ɾίʔυϨϏϡʔແ͠ ɾίʔσΟϯάن໿ແ͠ ɾಈ͚͹ྑ͍΍ঢ়ଶ ɾ΄΅࢓༷ॻແ͠

  15. 1 5 2 ίʔυͷओͳ໰୊఺ Կ͔ҰͭίʔυΛ͍ͬͨ͡ΒɺσάϨ͠΍͍͢δΣ ϯΨঢ়ଶ ɾPHPUnitͷςετ͕Ұͭ΋ͳ͍ ɾϚδοΫφϯόʔ͕େྔʹ࢖ΘΕ͍ͯΔ ɾifจͷωετ(5ॏͱ͔) ɾ਺ࣈ͕stringͱnumberͰೖͬͯ͘Δ

    ɾఆ਺͕΄΅࢖ΘΕ͍ͯͳ͍ ɾSQL͕௚ॻ͖
  16. 3 LaravelͰͷϦϑΝΫλํ਑

  17. 1 7 Fat Controller ͱ͸ʁ ɾFatControllerʢଠͬͨίϯτϩʔϥʣͷ͜ͱͰ͢ɻͳΜͰଠΔ͔ͱݴ͏ͱ୲͏੹຿͕ଟ͍ ͔ΒͰ͢ɻྫ͑͹ɺԼهͷΑ͏ͳཧ༝͕ߟ͑ΒΕ·͢ɻ ɾෳ਺ͷϝιουͰॲཧ͕ॏෳ͍ͯ͠Δɻ ɾ୯७ʹΞΫγϣϯϝιου͕ଟ͍ɻ ɾϩδοΫ͕ଟ͗͢Δɻ

    ɾͻͱͭͷมߋ͕ଞͷ෦෼ʹٴ΅͢ӨڹΛݕ஌ͮ͠Β͍ঢ়ଶʹ͋Δ
  18. 1 8 Fat Controller͸ԿͰྑ͘ͳ͍ͷ͔ʁ ɾControllerͷ੹຿ͷൣғ֎ͷॲཧ΋ೖ͖ͬͯͯ͠·͏ɻ Controllerͷ੹຿͸ - ೖྗʢHTTPϦΫΤετʣΛड͚औΓɺνΣοΫ͢Δ - ద੾ͳॲཧܥ΁஋Λ౉͢

    - ϨεϙϯεΛฦ͢ ͷ̏ͭͱݴΘΕ͍ͯ·͢ɻ ɾҰͭҰͭͷॲཧ͕௕ͯ͘ɺॲཧΛ௥͏ͷ͕೉͍͠ɻ ɾॲཧͷॏෳ͕ଟ͍ͷͰɺ࢓༷มߋ͕͋ͬͨ࣌ʹमਖ਼͕೉͍͠ɻ https://eh-career.com/engineerhub/entry/2021/05/28/160000#Laravel%E3%81%AE%E4%BE%8B%E3%81%AB%E8%A6%8B%E3%82%8BFatModel
  19. શମߏ੒(ཧ૝) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model Database όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻
  20. શମߏ੒(ཧ૝) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model Database όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻ ControllerͰग़͖ͯ ͨϩδοΫΛmodel ʹدͤΔ
  21. શମߏ੒(ཧ૝) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model Database όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻ ֎෦αʔϏεΛୟ͘ Α͏ͳॲཧ͸Service ʹॻ͘
  22. શମߏ੒(ཧ૝) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model Database όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻ DBͰఆٛ͢Δ΄ͲͰ ͸ͳ͍Α͏ͳఆ਺ܥ ͸Enumʹॻ͘
  23. શମߏ੒(ཧ૝) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model Database όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻ ό Ϧ σ ʔ γ ϣ ϯ ͸ RequestΫϥεʹॻ͘
  24. શମߏ੒(΋ͬͱେن໛ʹͳ͖ͬͯͨΒ) αʔόαΠυ ϒϥ΢β͔Β ΞΫηε Component HTML CSS JS bladeͳͲ Resouce.php

    APIϦΫΤετ APIϨεϙϯε Request.php router controller Model όϦσʔγϣϯ Ϩεϙϯε஋Λ ఆٛ Service αʔυύʔςΟ MPA Events Enum ਵ࣌࢖༻ Repository Logic Ϗδωεɾ ϩδοΫ σʔλɾ ΞΫηε Database
  25. 4 ϦϑΝΫλ಺༰

  26. 2 6 ᶃϚδοΫφϯόʔΛఆ਺Ͱఆٛ͢Δ ϚδοΫφϯόʔΛmodel΋͘͠͸ EnumΫϥεʹఆ਺ͱͯ͠ఆٛͯ͠ ݺͼग़͢ɻ

  27. 2 7 ᶄprivateϝιουʹ੾Γ෼͚Δ Ұͭͷϝιου͕50ߦΛ௒͑ͦ͏ͳ Βɺॲཧ͝ͱʹprivateϝιουʹ෼ ͚Δ

  28. 2 8 ᶅmodelΫϥεʹϩδοΫΛ෼͚Δ ControllerʹϩδοΫ͕૿͖͑ͯͨ ΒɺmodelʹॲཧΛ෼͚Δ

  29. 2 9 ᶆόϦσʔγϣϯ͸RequestΫϥεʹॻ͘ ControllerʹόϦσʔγϣϯΛ௚ॻ ͖͠ͳ͍ͰRequestΫϥεͰॲཧ͢ Δ

  30. 3 0 ᶇෳ਺ͷ Controller ʹ෼཭͢Δ ҰͭͷControllerͰΞΫγϣϯϝ ι ο υ ͕

    ૿ ͑ ͢ ͗ Δ ৔ ߹ ͸ ɺ ControllerϑΝΠϧ͝ͱ෼͚Δ
  31. 3 1 ᶈϨεϙϯεͷσʔλΛResource ʹॻ͘ Ϩεϙϯεσʔλ͕ෳࡶԽ͖ͯͨ͠ ΓɺՃ޻͕ඞཁͳ৔߹͸ResouceΫ ϥεʹ෼͚Δ

  32. 3 2 ·ͱΊ ɾControllerͷ੹຿ͷൣғ֎ͷॲཧ͸ೖΕͳ͍ɻ ɾඞཁʹԠͯ͡ɺϩδοΫͷ૚Λ૿΍͢ɻ ɾॲཧΛࡉ෼Խͯ͠ɺॏෳ͢Δॲཧ͸·ͱΊΔɻ

  33. 3 3 ࢀߟ https://qiita.com/karayok/items/d7740ab2bd0adbab2e06 https://qiita.com/nunulk/items/2323b0c30a2cbe5fdb0c https://qiita.com/nunulk/items/6ed409345efb6ee4f660 ϦϙδτϦύλʔϯͱ Laravel ΞϓϦέʔγϣϯͰͷσΟϨΫτϦߏ଄ ࠓ೔͔Β࢝ΊΒΕΔϦϑΝΫλϦϯά10બ

    (1) Laravel Ͱ Fat Controller Λ๷͙ 5 ͭͷ Tips