問題描述
根據 Laravel 4 文檔.
作曲家是:
視圖合成器是在渲染視圖時調用的回調或類方法.如果每次在整個應用程序中呈現視圖時都希望將數據綁定到給定視圖,則視圖編寫器可以將該代碼組織到一個位置.因此,視圖編輯器的功能可能類似于視圖模型"或演示者".
View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want bound to a given view each time that view is rendered throughout your application, a view composer can organize that code into a single location. Therefore, view composers may function like "view models" or "presenters".
View::composer('profile', function($view)
{
$view->with('count', User::count());
});
還有
創作者是:
視圖創建者的工作方式幾乎與視圖編輯器完全一樣;然而,當視圖被實例化時,它們會立即被觸發.注冊一個視圖創建者,簡單使用creator方法
View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. To register a view creator, simple use the creator method
View::creator('profile', function($view)
{
$view->with('count', User::count());
});
所以問題是:有什么區別?
推薦答案
當您使用 View::creator
時,您有機會覆蓋控制器中的視圖變量.像這樣:
When you use View::creator
you have the chance to override the variables of view in the controller. Like this:
View::creator('layout', function($view) {
$view->with('foo', 'bar');
});
// in controller
return View::make('layout')->with('foo', 'not bar at all');
// it's defined as 'not bar at all' in the view
-
View::composer('hello', function($view) {
$view->with('foo', 'bar');
});
// in controller
return View::make('hello')->with('foo', 'not bar at all');
// it's defined as 'bar' in the view
這篇關于Laravel 中 View Composer 和 Creator 的區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!