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
410
ビューコンポーザーでコントローラをキレイにする
2019/01/26 PHPカンファレンス仙台のLTで発表したスライドです。
chiroruxx
January 26, 2019
Tweet
Share
More Decks by chiroruxx
See All by chiroruxx
PHPはいつから死んでいるかの調査
chiroruxx
2
440
元phperから見たGoの良いところ
chiroruxx
0
21
Go Connectへの想い
chiroruxx
0
180
GraphQLに入門してみた
chiroruxx
2
210
ドキュメンテーションコメント再入門
chiroruxx
0
100
我流カンファレンス楽しみ術
chiroruxx
0
63
最初の一歩を踏み出す言葉
chiroruxx
4
1.1k
PhpStormをIDEとして使う
chiroruxx
0
64
Goを始めて感じたPHPの魅力
chiroruxx
1
68
Other Decks in Technology
See All in Technology
Laboratories in Science and Technology: Deep Neural Networks
keio_smilab
PRO
3
180
Databricksの生成AI戦略
taka_aki
1
380
Security Hubのセキュリティスコアはどうやって計算されるか
toru_kubota
0
110
PhpStorm超絶技巧40分集中講義 #phpconkagawa
yusuke
4
790
スクラムに出会って「できた」を実感できるようになってきた話 / Scrum makes me feel like I can do it
yayoi_dd
2
110
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
38k
株式会社EventHub・エンジニア採用資料
eventhub
0
2.1k
Kaggleで学ぶ系列データのための深層学習モデリング
yu4u
7
1.7k
Taking Flight with Tailwind CSS
opdavies
0
4.3k
生成AIがもたらす変革 / GitHubGalaxy_CyberAgent
cyberagentdevelopers
PRO
2
180
動画配信サービスのフロントエンド実装に学ぶ設計原則
yud0uhu
1
140
試作とデモンストレーション / Prototyping and Demonstrations
ks91
PRO
0
180
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
The Invisible Customer
myddelton
114
12k
Adopting Sorbet at Scale
ufuk
69
8.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
245
20k
RailsConf 2023
tenderlove
9
590
Optimizing for Happiness
mojombo
371
69k
Bash Introduction
62gerente
605
210k
KATA
mclloyd
16
12k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
358
22k
Building Your Own Lightsaber
phodgson
100
5.7k
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); }
まとめ ▪ ビューコンポーザーを使うと 簡単にビューの処理を分離できるよ! ▪ は関係ないよ!