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
実運用におけるLaravelとNuxtでのRepositoryのレイヤ分割の話
Search
Shohei Kondo
January 31, 2019
Programming
3
3.6k
実運用におけるLaravelとNuxtでのRepositoryのレイヤ分割の話
Laravel/Vue.js勉強会#7 資料
Twitter:
https://twitter.com/korn_shonery
Blog:
https://blog.kon-shou.com/
Shohei Kondo
January 31, 2019
Tweet
Share
More Decks by Shohei Kondo
See All by Shohei Kondo
Serverless 入門 ~環境構築からデプロイまで~
kon_shou
0
340
Other Decks in Programming
See All in Programming
Introduction to Git & GitHub
latte72
0
110
あのころの iPod を どうにか再生させたい
orumin
2
2.5k
Scale out your Claude Code ~自社専用Agentで10xする開発プロセス~
yukukotani
9
2.3k
DataformでPythonする / dataform-de-python
snhryt
0
180
マイコンでもRustのtestがしたい その2/KernelVM Tokyo 18
tnishinaga
2
2.3k
あまり知られていない MCP 仕様たち / MCP specifications that aren’t widely known
ktr_0731
0
280
サイトを作ったらNFCタグキーホルダーを爆速で作れ!
yuukis
0
390
Google I/O recap web編 大分Web祭り2025
kponda
0
2.9k
WebAssemblyインタプリタを書く ~Component Modelを添えて~
ruccho
1
870
LLMは麻雀を知らなすぎるから俺が教育してやる
po3rin
3
2.2k
AIエージェント開発、DevOps and LLMOps
ymd65536
1
270
ゲームの物理
fadis
5
1.2k
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
126
53k
It's Worth the Effort
3n
187
28k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.6k
Visualization
eitanlees
146
16k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Pragmatic Product Professional
lauravandoore
36
6.8k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
Mobile First: as difficult as doing things right
swwweet
223
9.9k
How to train your dragon (web standard)
notwaldorf
96
6.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Fireside Chat
paigeccino
39
3.6k
Docker and Python
trallard
45
3.5k
Transcript
実運⽤におけるLaravel とNuxt での Repository のレイヤ分割の話 @Laravel/Vue.js 勉強会#7 By kon-shou
⾃⼰&会社紹介 ⾃⼰ Laravel: 1 年7 ヶ⽉ Nuxt: 11 ヶ⽉ 会社
会社名: オープンロジ 業種: 物流アウトソーシング Laravel+react x1 Laravel+Nuxt.js x2
アジェンダ 1. よく⾒るレイヤー構造の話 1min 2. オープンロジの場合の話 7min 3. Pros/ Cons
2min
(たぶん)よく⾒る画像
なるほど... ? で、実際どうやんの?
オープンロジの場合 届いた荷物(Warehousing) を システムで⾒たい 届いた荷物にID (WarehousingId) が貼ってあるよ!
データフローの概略図
Nuxt の世界のお話
pages/warehousings/_id.vue pages ├── home ├── items │ └── _id ├──
schedules │ └── warehousings │ └── _id │ └── fix ├── shippings │ └── _id └── warehousings └── _id ブラウザから https://openlogi.web.example.com/warehousings/${warehousingId} にアクセス
warehousings/_id/index のasyncData() が呼ばれる async asyncData({ app, params, query }) {
const warehousingRepository = app.$serviceProvider.get('WarehousingRepository'); const warehousingId = _.get(params, 'id'); const warehousing = await warehousingRepository.find(warehousingId); return { warehousing, }; } 真っ先に呼ばれる asyncData() で warehousingRepository. nd(warehousingId) が実⾏される
warehousingRepository.find() async find(warehousingId): Promise<Warehousing> { const response = await this.axios.get(`/warehousings/${warehousingId}`);
return new Warehousing(response.data); } nuxt.con g.js で this.axios.get をapi サーバーに向けている => https://openlogi.api.example.com/warehousings/${warehousingId} が叩かれる
ここからLaravel の世界のお話
今どこの話してるんだ... ? => Laravel/WarehousingContoller から Mysql を⽬指す
H p/Controllers/WarehousingController routing を経て show() に到達 public function show($warehousingId): Warehousing
{ $warehousing = $this->warehousingRepository->find($warehousingId); if (!$warehousing) { throw new HttpNotFoundException(' 指定された⼊荷が⾒つかりませんでした'); } return $warehousing; } Laravel 側でも、再度 warehousingRepository を呼ぶ
Domains/Warehousing/WarehousingRepository public function find($id) { return Warehousing::find($id); } ここでやるのは、モデルを⽣成して返すだけ 今のところ
は... ( 伏線)
今どこの話してるんだ... ?
En tyPresenters/Domains/Warehousing/ WarehousingPresenter public function toApi($warehousing): array { $array =
$warehousing->toArray(); $array['warehousing_items'] = $warehousing->warehousingItems ->map(function (WarehousingItem $warehousingItem) { return $this->warehousingItemPresenter->toApi($warehousingItem); } ); return $array; } 帰ってきたモデルをいい感じに 整形 する
ふたたびNuxt の世界のお話
再掲 warehousingRepository.find() async find(warehousingId): Promise<Warehousing> { const response = await
this.axios.get(`/warehousings/${warehousingId}`); return new Warehousing(response.data); } Laravel の WarehousingPresenter で整形したデータ (=response.data) からモデルを作る => そのモデルを pages/warehousings/_id/index に返す
再掲 warehousings/_id/index.vue async asyncData({ app, params, query }) { const
warehousingRepository = app.$serviceProvider.get('WarehousingRepository'); const warehousingId = _.get(params, 'id'); const warehousing = await warehousingRepository.find(warehousingId); return { warehousing, }; } ここで帰るwarehousing を template タグ内で描写する。完!
あれ?何か良いことあるんだっけ?
実は、本当はrepository から⾊々と呼んでます
メリット 変更が発⽣した場合に、 修正範囲が狭まる データを取得するソース(DB/ElasticSearch/API/etc... )の変更が⽣ じた場合に、Repository だけを修正すればいい フロントに渡すデータを整形したい場合に、Presenter だけを修正す ればいい
カッコよく⾔えば プログラムの責務分担を明確にできる
デメリット 初⾒殺し Join したばかりだとキャッチアップコストが上がっちゃう... 実装量が増えがち ドメインの切り⽅に迷う 「Warehousing のサブクラスは別クラスでやるべき?それとも Warehousing と同じ??」
でもメリットでかいよ! みんなも⾊々考えて Clean なArchitecture を⽬指していこうぜ!