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
490
ビューコンポーザーでコントローラをキレイにする
2019/01/26 PHPカンファレンス仙台のLTで発表したスライドです。
chiroruxx
January 26, 2019
Tweet
Share
More Decks by chiroruxx
See All by chiroruxx
Go Connectへの想い
chiroruxx
0
170
eBPF with PHPをさわる
chiroruxx
0
120
sl完全に理解したつもり
chiroruxx
0
100
命名をリントする
chiroruxx
1
780
良い命名かを調べるリンターを作った + α
chiroruxx
0
110
GoLandを布教する会
chiroruxx
0
36
PHPはいつから死んでいるかの調査
chiroruxx
3
650
元phperから見たGoの良いところ
chiroruxx
0
93
Go Connectへの想い
chiroruxx
0
480
Other Decks in Technology
See All in Technology
さくらのIaaS基盤のモニタリングとOpenTelemetry/OSC Hokkaido 2025
fujiwara3
2
270
AWS Organizations 新機能!マルチパーティ承認の紹介
yhana
1
230
AWS Summit Japan 2025 Community Stage - App workflow automation by AWS Step Functions
matsuihidetoshi
1
310
なぜ私はいま、ここにいるのか? #もがく中堅デザイナー #プロダクトデザイナー
bengo4com
0
1.3k
自律的なスケーリング手法FASTにおけるVPoEとしてのアカウンタビリティ / dev-productivity-con-2025
yoshikiiida
0
1.1k
「Chatwork」の認証基盤の移行とログ活用によるプロダクト改善
kubell_hr
1
240
OPENLOGI Company Profile for engineer
hr01
1
33k
本が全く読めなかった過去の自分へ
genshun9
0
720
Tech-Verse 2025 Global CTO Session
lycorptech_jp
PRO
0
1.2k
開発生産性を組織全体の「生産性」へ! 部門間連携の壁を越える実践的ステップ
sudo5in5k
0
670
Liquid Glass革新とSwiftUI/UIKit進化
fumiyasac0921
0
300
Tech-Verse 2025 Keynote
lycorptech_jp
PRO
0
1.4k
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
3.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
138
34k
Documentation Writing (for coders)
carmenintech
72
4.9k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Rails Girls Zürich Keynote
gr2m
94
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
48
5.4k
Raft: Consensus for Rubyists
vanstee
140
7k
How STYLIGHT went responsive
nonsquared
100
5.6k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
940
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
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); }
まとめ ▪ ビューコンポーザーを使うと 簡単にビューの処理を分離できるよ! ▪ は関係ないよ!