Laravel 中 View Composer 和 Creator 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19265237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between View Composer and Creator in Laravel?
提问by Pars
According to Laravel 4 documentation.
Composer is:
作曲家是:
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());
});
And
和
Creator is:
创造者是:
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
视图创建者的工作方式几乎与视图作曲家完全一样;然而,当视图被实例化时,它们会立即被触发。要注册一个视图创建者,简单地使用 creator 方法
View::creator('profile', function($view)
{
$view->with('count', User::count());
});
So the question is : What is the difference?
所以问题是:有什么区别?
回答by Hkan
When you use View::creator
you have the chance to override the variables of view in the controller. Like this:
当您使用时,View::creator
您有机会覆盖控制器中的视图变量。像这样:
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
回答by Laurence
It took me a while to work this out, I had to dig in the source code to work it out. The difference is at what point in the cycle of the Laravel application you want the command to run.
我花了一段时间来解决这个问题,我不得不挖掘源代码来解决这个问题。不同之处在于您希望命令在 Laravel 应用程序循环中的哪个点运行。
There are a number of points in the Laravel cycle involving views.
Laravel 循环中有许多涉及视图的点。
You can make a view using View::make()
. This is when a view is instantiated - and during the View::make()
command any View::creators()
are called, before the function is returned.
您可以使用View::make()
. 这是实例化视图的时间 - 并且在View::make()
命令期间View::creators()
调用any ,在返回函数之前。
Normally you just run return View::make()
- which means the view is 'created', and then returned to the Laravel core where it is then 'composed' to screen. This is when the View::composer()
is called (i.e. after the view has returned).
通常你只是运行return View::make()
- 这意味着视图被“创建”,然后返回到 Laravel 核心,然后它被“组合”到屏幕上。这是在View::composer()
调用 时(即在视图返回之后)。
I'm not sure why you would want to use one or the other, but that explains the difference between the two.
我不确定您为什么要使用其中之一,但这解释了两者之间的区别。
回答by DBCrocky
Another difference is that an Exception thrown within a ViewCreator will bubble back up to the Controller. This is handy for authorizations. In the ViewCreator you can get permissions data, then if the user is not authorized for that page, throw an exception and let the controller handle it. For example:
另一个区别是在 ViewCreator 中抛出的异常将冒泡回控制器。这对于授权很方便。在 ViewCreator 中,您可以获取权限数据,然后如果用户未获得该页面的授权,则抛出异常并让控制器处理它。例如:
class MyController {
public function MyAction {
try {
return view('my_view');
} catch (\Exception $e) {
echo "<h1>Exception</h1>";
echo $e->getMessage();
}
}
}
class MyViewCreator {
public function create(View $view) {
$loggedInUser = User::with('permissions')->find(Auth::user()->id);
if (! $loggedInUser->hasPermission('MY_PERMISSION')) {
throw new \Exception("You are not authorized");
}
...
}
}