Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ServiceとRepository
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
ショウノシオリ
April 12, 2017
Programming
36
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ServiceとRepository
2017/4/12 関西PHPユーザーズグループの勉強会での登壇資料です。
ServiceとRepositoryについて自分なりに解釈してみたことをまとめました。
ショウノシオリ
April 12, 2017
More Decks by ショウノシオリ
See All by ショウノシオリ
Nuxt / Vue 開発でやりがちな 「読みづらい」「わかりづらい」コード
sshono1210
0
280
開発チームのリーダーとしてどうあるべきか?
sshono1210
3
1.3k
Nuxt.js のディレクトリ
sshono1210
0
3k
Nuxt.js で SSR 対応する
sshono1210
1
2.3k
array_merge と array_push の違いについて
sshono1210
0
570
全くデザインを勉強したことのないエンジニアが「なるほどデザイン」を読んで少しだけ勉強した話
sshono1210
0
270
Vue.js の methods と computed
sshono1210
0
150
すぐに使える ES2015 の基本構文3つ
sshono1210
0
110
肌で感じたディレクションとマネジメント
sshono1210
0
110
Other Decks in Programming
See All in Programming
Foundry Localでエージェント開発
seosoft
0
190
LLMは4年分のCompose移行を再現できるのか?実プロダクト279件のXMLで探る自動化の境界線
makun
0
780
Security issues being discussed on Web Platforms
petamoriken
0
870
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
150
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
460
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
270
{ Android | Kotlin } Gradle Plugin in 2026
ryunen344
1
310
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
110
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
110
Intent as Code
shoppingjaws
6
980
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
540
DroidKaigi 2026 「個人開発という実験場: Android エンジニアが手にする4つの自由」
slashnephy
0
230
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Designing Experiences People Love
moore
143
24k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
930
The Cult of Friendly URLs
andyhume
79
7k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
How to build a perfect <img>
jonoalderson
1
6k
The Limits of Empathy - UXLibs8
cassininazir
1
660
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2.2k
Transcript
PHP勉強会 ServiceとRepository
自己紹介 ショウノシオリ ◦ 株式会社chatbox ◦ エンジニア ◦ @shoshoでQiita記事 書いてます ◦
Laravel と日々格闘
最近開発したもので出てきた Service と Repository がよくわからなかったので勉強してみました
開発したもの ▷ Laravel 5.4 ▷ ECサイト(管理画面のみ) ▷ フロントは他の会社 ▷ コードの設計は完了済みだった
開発に使う主なもの ▷ View ▷ Route ▷ Controller
と ▷ Service ▷ Repository ▷ Eloquent
DBにアクセスするまでの流れ Controller Service Repository Eloquent DB
DBまわりがなぜ こんなに細かく分けられているんだろう?
Service と Repository は必要なのか?
DBにアクセスするまでの流れが Controller Service Repository Eloquent DB だとすると
2つの問題点 ▷ DBの設計が変わったときに、 Eloquentを 使っているコントローラ全てを修正する必要 が出て来る ◦ めんどう ◦ 大変 ▷
フロントでも同じDB操作をすることがある のに、別々でつくるのはもったいない ◦ ユーザー情報の取得、とか
この2つを解決するのが Service と Repository
DBの設計が変わったときに、Eloquentを使っ ているコントローラ全てを修正する必要が出て 来る 問題
Repositoryが解決 ▷ Eloquentを操作する唯一のクラス。ここか ら先はEloquentではなくEntityとしてデータ を持ち回すようにしている。 ◦ DB変更に伴うEloquentの修正が与える 影響はリポジトリ内に収まる ◦ 修正箇所を見つけやすい
◦ 修正する場所も少なくて済む
フロントでも同じDB操作をすることがあるの に、別々でつくるのはもったいない 問題
Serviceが解決 ▷ 「ユーザー情報を取得したい」といった具体 的な要求に対する処理を提供するクラス。 ◦ 再利用できる ◦ 処理自体を固めることになるので、画面 の仕様変更などに強くなる
なので以下のような役割分担になっている のかな、と
Controller Service Repository Eloquent DB リクエストの受取と 結果を返す、だけ 要求に対するものを 提供するクラス Eloquentを管理する
唯一のクラス DBを操作する
このように 関心事 = ドメイン を中心に開発をすすめる方法が ドメイン駆動設計(DDD) 的な?
なぜこんなに細かくわけられているんだろう? の答えとしては
▷ フロントとの開発をみこしたものだから ▷ 各クラスにしっかり役割分担しておくことで 修正しやすくするため かなぁと思いました
つくる立場であっても 少しずつ設計思想についても学んでいきたいと 思います
Thanks! Any questions?