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
400
ビューコンポーザーでコントローラをキレイにする
2019/01/26 PHPカンファレンス仙台のLTで発表したスライドです。
chiroruxx
January 26, 2019
Tweet
Share
More Decks by chiroruxx
See All by chiroruxx
PHPはいつから死んでいるかの調査
chiroruxx
1
400
元phperから見たGoの良いところ
chiroruxx
0
19
Go Connectへの想い
chiroruxx
0
140
GraphQLに入門してみた
chiroruxx
2
200
ドキュメンテーションコメント再入門
chiroruxx
0
88
我流カンファレンス楽しみ術
chiroruxx
0
58
最初の一歩を踏み出す言葉
chiroruxx
4
1.1k
PhpStormをIDEとして使う
chiroruxx
0
58
Goを始めて感じたPHPの魅力
chiroruxx
1
65
Other Decks in Technology
See All in Technology
プラットフォームってつくることより計測することが重要なんじゃないかという話 / Platform Engineering Meetup #8
taishin
1
370
Além do else! Categorizando Pokemóns com Pattern Matching no JavaScript
wmsbill
0
640
APIファーストなプロダクトマネジメントの実践 〜SaaSus Platformでの例〜 / "Practicing API-First Product Management - An Example with SaaSus Platform
oztick139
0
110
競技としてのKaggle、役に立つKaggle
yu4u
4
1.9k
Cloud Native Java with Spring Boot (CNCF Aarhus, April 2024)
thomasvitale
1
180
開発生産性大幅アップ!Postman VS Code拡張機能
nagix
2
390
ServiceNow Knowledge Learning Rise up
manarobot
0
210
EMとして2023年度に頑張ったこと / What we did well in FY2023 as a EM
pauli
1
170
アクセシビリティを考慮したUI/CSSフレームワーク・ライブラリ選定
yajihum
2
1k
SIEMを用いて、セキュリティログ分析の可視化と分析を実現し、PDCAサイクルを回してみた
coconala_engineer
0
340
推しは推せるときに推せ! プロダクトにフィードバックしていこう
nakasho
0
320
Cypress or Playwright?
rainerhahnekamp
0
110
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
28
6.4k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
226
51k
Building Applications with DynamoDB
mza
88
5.6k
The Cult of Friendly URLs
andyhume
74
5.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
355
18k
Web Components: a chance to create the future
zenorocha
305
41k
Faster Mobile Websites
deanohume
299
30k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
Automating Front-end Workflow
addyosmani
1356
200k
Designing for humans not robots
tammielis
248
25k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
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); }
まとめ ▪ ビューコンポーザーを使うと 簡単にビューの処理を分離できるよ! ▪ は関係ないよ!