Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
FABRIC TOKYO での Laravel 開発アーキテクチャ(Laravel/Vue.j...
Search
SHUNSUKE Takahashi
October 28, 2019
Technology
0
4.2k
FABRIC TOKYO での Laravel 開発アーキテクチャ(Laravel/Vue.js勉強会#11)/ laravue-laravel-clean-architecture
FABRIC TOKYO での Laravel 開発アーキテクチャ 無理しない DDD と Clean Architecture での開発手法(Laravel/Vue.js勉強会#11)
SHUNSUKE Takahashi
October 28, 2019
Tweet
Share
Other Decks in Technology
See All in Technology
日本語プログラミングとSpring Bootアプリケーション開発 #kanjava
yusuke
2
340
消し忘れリソースゼロへ!私のResource Explorer活用法
cuorain
0
140
第27回クラウド女子会 ~re:Invent 振り返りLT会~ 宣言型ポリシー、使ってみたらこうだった!
itkr2305
0
290
あなたの興味は信頼性?それとも生産性? SREとしてのキャリアに悩むみなさまに伝えたい選択肢
jacopen
6
2.9k
現実的なCompose化戦略 ~既存リスト画面の置き換え~
sansantech
PRO
0
160
Power BI は、レポート テーマにこだわろう!テーマのティア表付き
ohata_ds
0
120
panicを深ぼってみる
kworkdev
PRO
2
140
教師なし学習の基礎
kanojikajino
4
350
srekaigi2025-hajimete-ippo-aws
masakichieng
0
240
HCP TerraformとAzure:イオンスマートテクノロジーのインフラ革新 / HCP Terraform and Azure AEON Smart Technology's Infrastructure Innovation
aeonpeople
3
980
“自分”を大切に、フラットに。キャリアチェンジしてからの一年 三ヶ月で見えたもの。
maimyyym
0
300
顧客の声を集めて活かすリクルートPdMのVoC活用事例を徹底解剖!〜プロデザ!〜
recruitengineers
PRO
0
200
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
A Tale of Four Properties
chriscoyier
157
23k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.8k
Site-Speed That Sticks
csswizardry
3
300
Music & Morning Musume
bryan
46
6.3k
Why Our Code Smells
bkeepers
PRO
335
57k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
BBQ
matthewcrist
85
9.4k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
We Have a Design System, Now What?
morganepeng
51
7.4k
Into the Great Unknown - MozCon
thekraken
34
1.6k
Transcript
FABRIC TOKYOͰͷ Laravel ։ൃΞʔΩςΫνϟ ແཧ͠ͳ͍%%%ͱ$MFBO"SDIJUFDUVSFͰͷ։ൃख๏
୭ ? ໊લߴڮढ़ี λΧϋγγϡϯεέ -BSBWFMྺ ʙ '"#3*$50:,0*ODΤϯδχΞ ελʔϑΣεςΟόϧגࣜձࣾΤϯδχΞ
1)1$POGFSFODF+"1"/࣮ߦҕһ ෭ҕһ 1)1FS,BJHJ࣮ߦҕһ
͞ͳ͍ࣄ w $SFBO"SDIJUFDUVSFͷৄ͍͠༰ w "QQMJDBUJPO4FSWJDF 1SFTFOUFSFUDʜ w 7VFKTͷ༰
Agenda w '"#3*$50,:0&$ͷٕज़ʹ͍ͭͯ w ͳͥɺ$MFBO"SDIJUFDUVSFͳͷ͔ w ࣮ࡍͷར༻ํ๏ w ར༻ͯ͠ྑ͘ͳͬͨ͜ͱ
FABRIC TOKYO ECͷٕज़ελοΫ w "NB[PO8FC4FSWJDF &$3%44$MPVE'SPOU w #"$,&/% w
1)1-BSBWFM YʹόʔδϣϯΞοϓத w '30/5&/% w 7VFKT K2VFSZ3FBDU
ݹ͖Α͖-".1ڥ
FABRIC TOKYO / Architecture
Clean Architecture??
Laravel (MVC) ։ൃͰͷΈ
Α͋͘ΔΈ w େྔͷॲཧ͕͋Δ.PEFM w େ͖͗͢Δ$POUSPMMFS w ϏδωεϩδοΫΛ࣋ͬͨ7JFX 5FNQMBUF w
ॻ͖ʹ͍͘5FTU
None
Clean Architecture
None
The Clean Architecture
Α͔͘Βͳ͍ͦ͠͏
͍͠
શͯΛཧղ͠Α͏ͱ͢Δ͔ΒͰʁ
શͯΛऔΓೖΕͳ͚ΕΑ͍ͷͰʁ
Α͋͘ΔΈ w େྔͷॲཧ͕͋Δ.PEFM w େ͖͗͢Δ$POUSPMMFS w ϏδωεϩδοΫΛ࣋ͬͨ7JFX 5FNQMBUF w
ॻ͖ʹ͍͘5FTU
ஔ͖͑ • Model -> Entity (& Repository) • Controller ->
UseCase • View -> Presenter
Entity
Entity l࠷ॏཁͷϏδωεϧʔϧͱ࠷ॏཁͷϏδωεσʔλີʹ݁ͼ͍ ͍ͯΔͨΊɺΦϒδΣΫτͷ༗ྗͳީิʹͳΔɻ͜͏ͨ͠ΦϒδΣΫ τͷ͜ͱΛΤϯςΟςΟͱݺͼ͍ͨɻz ʮ$MFBO"SDIJUFDUVSFୡਓʹֶͿιϑτΣΞͷߏͱઃܭʯ
UseCase
UseCase lΞϓϦέʔγϣϯݻ༗ͷϏδωεϧʔϧؚ͕·Ε͍ͯΔɻ͜͜ʹɺγεςϜ ͷͯ͢ͷϢʔεέʔε͕ΧϓηϧԽɾ࣮͞Ε͍ͯΔɻϢʔεέʔεɺΤϯ ςΟςΟʹೖग़ྗ͢ΔσʔλͷྲྀΕΛௐ͠ɺϢʔεέʔεͷඪΛୡͰ͖Δ Α͏ʹɺΤϯςΟςΟʹ࠷ॏཁϏδωεϧʔϧΛ༻͢ΔΑ͏ʹࢦࣔΛग़͢ɻz ʮ$MFBO"SDIJUFDUVSFୡਓʹֶͿιϑτΣΞͷߏͱઃܭʯ
ͭ·Γʁ • Entity (Model) ʹΞϓϦέʔγϣϯ߹ͷॲཧΛೖΕͳ͍ɻ • ΞϓϦέʔγϣϯ߹ͷॲཧ UseCase Ͱߦ͏͖Ͱ͋Δɻ
ྫ1045͞ΕͨϢʔβʔใΛ%#ʹอଘ͢Δ
༰ w OBNF FNBJM QBTTXPSEΛೖྗʢईͷ߹্7BMJEBUJPOআ͘ʣ w QBTTXPSE҉߸Խ
Կߟ͑ͣʹॻ͘ͱ…
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 }
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 }
ࠓͳͯ͘… w ޙʹॲཧ͕૿͑Δࣄ͕ఆ͞ΕΔ w ҉߸Խ͢Δॲཧͱอଘ͢Δॲཧ͋Δ w ςετͮ͠Β͍
Let’s Try. https://github.com/stakahashi/example-laravel-clean-architecture
UsersController
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 }
UseCase
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 }
UserRepository
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 }
UserEntity
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 }
֤߲ͷׂ w $POUSPMMFS w ϦΫΤετ͔ΒσʔλΛ6TF$BTFʹ͠ɺϨεϙϯεΛฦ͢ w 6TF$BTF w σʔλΛड͚औͬͯՃʢQBTTXPSEͷ)BTIԽʣΛߦ͍ɺ3FQPTJUPSZ͢ w
3FQPTJUPSZ w %#ʹड͚औͬͨσʔλΛॻ͖ࠐΉ w &OUJUZ w 6TFSࣗମͷ࣮ମ
Կ͕ྑ͘ͳͬͨͷ͔ʁ
ྑ͘ͳͬͨ͜ͱ w $POUSPMMFS w ॲཧ6TF$BTFʹ͋ΔͷͰബ͘ͳΔɻ w ϦΫΤετͱϨεϙϯεΛؾʹ͢ΕΑ͍ɻ w .PEFM &OUJUZ
w ΞϓϦέʔγϣϯʹґଘ͢Δॲཧ6TF$BTF͕ߦ͏ͷͰঢ়ଶͱͯ͠ѻ͍͍͢ w %#ͷॲཧ3FQPTJUPSZΛհͯ͠ߦ͏ͷͰॲཧࣗମ3FQPTJUPSZʹΓɺ.PEFM͕ബ͘ͳΔɻ w 5FTU w ֤ॲཧ͕খ͘͘͢͞͠ͳΔͷͰ୯ମͰͷςετ͕༰қʹͳΔ
࠷ޙʹ
࠷ޙʹ w -BSBWFMΛී௨ʹॻ͘ͱະདྷۤ͘͠ͳΓ͕ͪͳͷͰ %%%$MFBO"SDIJUFDUVSFଉͷ͍ΞϓϦέʔγϣϯΛ։ ൃ͢ΔࡍͷΞϓϩʔνͱͯ͠ݕ౼ͨ͠΄͏͕͍͍ͱࢥ͏ɻ w $MFBO"SDIJUFDUVSFͷશͯΛऔΓೖΕͣͱ෦తʹ༻͢Δ ͜ͱͰԸܙड͚ΒΕΔɻ w νʔϜ։ൃͰͷ$MFBO"SDIJUFDUVSFͷಋೖʹϋʔυϧΛ
Լ͛ͯελʔτ͢Δ͜ͱͰ͖Δɻ
ແཧͳ͘%%%$MFBO"SDIJUFDUVSFʹ ৮ΕͯΈΑ͏
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ