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
81
sl完全に理解したつもり
chiroruxx
0
75
命名をリントする
chiroruxx
1
680
良い命名かを調べるリンターを作った + α
chiroruxx
0
77
GoLandを布教する会
chiroruxx
0
23
PHPはいつから死んでいるかの調査
chiroruxx
3
610
元phperから見たGoの良いところ
chiroruxx
0
73
Go Connectへの想い
chiroruxx
0
440
GraphQLに入門してみた
chiroruxx
2
310
Other Decks in Technology
See All in Technology
白金鉱業Meetup Vol.17_あるデータサイエンティストのデータマネジメントとの向き合い方
brainpadpr
7
990
脳波を用いた嗜好マッチングシステム
hokkey621
0
280
Snowflakeの開発・運用コストをApache Icebergで効率化しよう!~機能と活用例のご紹介~
sagara
1
350
デスクトップだけじゃないUbuntu
mtyshibata
0
690
ソフトウェアエンジニアと仕事するときに知っておいたほうが良いこと / Key points for working with software engineers
pinkumohikan
1
140
エンジニアリング価値を黒字化する バリューベース戦略を用いた 技術戦略策定の道のり
kzkmaeda
6
1.8k
php-conference-nagoya-2025
fuwasegu
0
140
1行のコードから社会課題の解決へ: EMの探究、事業・技術・組織を紡ぐ実践知 / EM Conf 2025
9ma3r
8
2.7k
JEDAI Meetup! Databricks AI/BI概要
databricksjapan
0
300
MIMEと文字コードの闇
hirachan
2
1.4k
短縮URLをお手軽に導入しよう
nakasho
0
140
エンジニアが加速させるプロダクトディスカバリー 〜最速で価値ある機能を見つける方法〜 / product discovery accelerated by engineers
rince
4
540
Featured
See All Featured
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
570
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
27
1.9k
Practical Orchestrator
shlominoach
186
10k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.5k
Music & Morning Musume
bryan
46
6.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
10
500
Git: the NoSQL Database
bkeepers
PRO
427
65k
Writing Fast Ruby
sferik
628
61k
GraphQLとの向き合い方2022年版
quramy
44
13k
BBQ
matthewcrist
87
9.5k
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); }
まとめ ▪ ビューコンポーザーを使うと 簡単にビューの処理を分離できるよ! ▪ は関係ないよ!