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
ビューコンポーザーでコントローラをキレイにする
Search
chiroruxx
January 26, 2019
Technology
0
470
ビューコンポーザーでコントローラをキレイにする
2019/01/26 PHPカンファレンス仙台のLTで発表したスライドです。
chiroruxx
January 26, 2019
Tweet
Share
More Decks by chiroruxx
See All by chiroruxx
eBPF with PHPをさわる
chiroruxx
0
83
sl完全に理解したつもり
chiroruxx
0
76
命名をリントする
chiroruxx
1
680
良い命名かを調べるリンターを作った + α
chiroruxx
0
79
GoLandを布教する会
chiroruxx
0
25
PHPはいつから死んでいるかの調査
chiroruxx
3
610
元phperから見たGoの良いところ
chiroruxx
0
75
Go Connectへの想い
chiroruxx
0
450
GraphQLに入門してみた
chiroruxx
2
320
Other Decks in Technology
See All in Technology
遷移の高速化 ヤフートップの試行錯誤
narirou
6
1.9k
LINEギフトにおけるバックエンド開発
lycorptech_jp
PRO
0
390
OPENLOGI Company Profile
hr01
0
60k
JAWS FESTA 2024「バスロケ」GPS×サーバーレスの開発と運用の舞台裏/jawsfesta2024-bus-gps-serverless
ma2shita
3
290
Platform Engineeringで クラウドの「楽しくない」を解消しよう
jacopen
4
150
Global Databaseで実現するマルチリージョン自動切替とBlue/Greenデプロイ
j2yano
0
150
急成長する企業で作った、エンジニアが輝ける制度/ 20250227 Rinto Ikenoue
shift_evolve
0
190
アジャイルな開発チームでテスト戦略の話は誰がする? / Who Talks About Test Strategy?
ak1210
1
690
IoTシステム開発の複雑さを低減するための統合的アーキテクチャ
kentaro
1
120
事業を差別化する技術を生み出す技術
pyama86
2
440
Охота на косуль у древних
ashapiro
0
120
Amazon Q Developerの無料利用枠を使い倒してHello worldを表示させよう!
nrinetcom
PRO
2
120
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
6
260
Git: the NoSQL Database
bkeepers
PRO
428
65k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
How GitHub (no longer) Works
holman
314
140k
A designer walks into a library…
pauljervisheath
205
24k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.1k
Build The Right Thing And Hit Your Dates
maggiecrowley
34
2.5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
How STYLIGHT went responsive
nonsquared
99
5.4k
4 Signs Your Business is Dying
shpigford
183
22k
RailsConf 2023
tenderlove
29
1k
Transcript
ビューコンポーザーで コントローラを キレイにする 01/26 PHPカンファレンス仙台 前田 和人
自己紹介 ▪ 前田 和人 ▪ @chiroruxxxx ▪ 弁護士ドットコム株式会社 ▪ ペチカ堂が好き
注意事項 ▪ Laravelベースで話をすすめます ▪ 他のFWでも似たような機能があるので、 脳内置換してください Symfony Embed controller in
Template Cake PHP View cell Yii Widget
本題
最初はキレイだったコードも・・・ ▪ ブログの記事ページで例えます public function show(Post $post) { return view('posts.show',
compact('post')); }
度重なる仕様追加によって・・・ カテゴリーの一覧を 出したい 検索フォームあったら 便利だよね ログイン導線も 関連記事も
長くなりますよね・・・ public function show(Post $post) { $categories = Category::all(); foreach
($categories as $key => $category) { $category->count = Category::where('id', $category->id)->count(); $categories->put($key, $category); } $searchFormAttributes = [ 'type' => PostType::all()->pluck('value', 'id'), 'max' => 120, 'categories' => $categories->push(['name' => 'なし', 'id' => ''])->pluck('name', 'id'), ]; if (Auth::check()) { $user = Auth::user(); } else { $user = new User; } $relatedPosts = $post->relatedPosts; if ($relatedPosts->count() < 5) { $addPosts = Post::orderBy('id', 'desc')->take(5 - $relatedPosts->count())->get(); $relatedPosts = $relatedPosts->merge($addPosts); } return view( 'posts.show’, compact('posts', 'categories', 'searchFormAttributes', 'user', 'relatedPost’) ); }
public function show(Post $post) { $categories = Category::all(); foreach ($categories
as $key => $category) { $category->count = Category::where('id', $category->id)->count(); $categories->put($key, $category); } $searchFormAttributes = [ 'type' => PostType::all()->pluck('value', 'id'), 'max' => 120, 'categories' => $categories->push(['name' => 'なし', 'id' => ''])->pluck('name', 'id'), ]; if (Auth::check()) { $user = Auth::user(); } else { $user = new User; } $relatedPosts = $post->relatedPosts; if ($relatedPosts->count() < 5) { $addPosts = Post::orderBy('id', 'desc')->take(5 - $relatedPosts->count())->get(); $relatedPosts = $relatedPosts->merge($addPosts); } return view( 'posts.show’, compact('posts', 'categories', 'searchFormAttributes', 'user', 'relatedPost’) ); } 長くなりますよね・・・ カテゴリー一覧 検索フォーム ログイン関係 関連記事
設計は難しい ▪ 「チョットデキル」達の言うことはわかる – モデルに処理を書け! – サービスクラスを作れ! ▪ シンプルな設計を習得するのは難しい –
チームの技術力によって最適解は違いそう ▪ 簡単にコントローラーの処理をキレイにしたい
ビューコンポーザー のご提案
ビューコンポーザーとは ▪ ビューコンポーザはビューがレンダーされる時に呼び出 される、コールバックかクラスメソッドのことです。 ビューがレンダーされるたびに結合したい情報があるな ら、ビューコンポーザがロジックを一箇所にまとめるの に役立ちます。 – https://readouble.com/laravel/5.5/ja/views.html ▪
なるほどわからん。
▪ 今まで コントローラ ビュー ビューコンポーザーの概要 検索フォーム 記事 カテゴリー ログイン導線 関連記事
▪ ビューコンポーザーを使うと ビュー ビューコンポーザーの概要 検索フォーム 記事 カテゴリー ログイン導線 関連記事 コンポーザー
コンポーザー コンポーザー コンポーザー コントローラ
ビューのレンダリング 内部処理の流れ コントローラから ビューを呼び出す サブビューを 呼び出す ViewComposerの 処理を実行する サブビューの レンダリング
ビューコンポーザーを使った結果 ▪ コントローラー public function show(Post $post) { return view('posts.show',
compact('post')); }
ビューコンポーザーを使った結果 ▪ コンポーザー(検索フォーム) public function compose(View $view) { $attributes =
[ 'type' => PostType::all()->pluck('value', 'id'), 'max' => 120, 'categories' => Category::all()-> //... ]; $view->with('attributes', $attributes); }
まとめ ▪ ビューコンポーザーを使うと 簡単にビューの処理を分離できるよ! ▪ は関係ないよ!