Laravel:View::share() 和 View::composer() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18315831/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:18:11  来源:igfitidea点击:

Laravel: Difference between View::share() and View::composer()

phpviewlaravel

提问by enchance

In relation to the question Passing default variables to view, to pass variables available among all views, is there a technical or functional difference between the use of View::composer():

关于将默认变量传递给视图、在所有视图之间传递可用变量的问题,使用以下方法之间是否存在技术或功能差异View::composer()

View::composer('*', function($view) {
    $thundercats = 'Woooooohh!!';
    $view->with('thundercats', $thundercats);
})

in the filters.phpfile or the use of View::share()in the BaseController.phpfile:

filters.php文件或使用View::share()BaseController.php文件:

public function __construct {
    $thundercats = 'Woooooohh!!';
    View::share('thundercats', $thundercats);
}

I've only recently learned about View::share()and find it exceptionally intruiging although I've already started using the former in another project.

View::share()尽管我已经开始在另一个项目中使用前者,但我最近才了解并发现它非常有趣。

Edit:

编辑:

My first assumption is that the former is a file (filters.php) while the the latter is a class (BaseController.php). With this in mind, I'm guessing a class is much better? Although, I'm not quite sure why at this point. :)

我的第一个假设是前者是一个文件(filters.php),而后者是一个类(BaseController.php)。考虑到这一点,我猜一个班级会好得多?虽然,我不太确定为什么在这一点上。:)

回答by tplaner

Technically they are not at all alike. View::sharesimply sets a variable, while View::composeris a callback function.

从技术上讲,它们完全不同。View::share简单地设置一个变量,View::composer而是一个回调函数。

Let me explain in greater detail:

让我更详细地解释一下:

View::shareis really straight forward it sets a variable which can be used within any of the views, think of it like a global variable.

View::share非常简单,它设置了一个可以在任何视图中使用的变量,把它想象成一个全局变量。

View::composerregisters an event which is called when the view is rendered, don't confuse it with a View::creatorwhich is fired when a view is instantiated.

View::composer注册一个在呈现视图时调用的事件,不要将它与View::creator实例化视图时触发的 a混淆。

View::composer/ View::creatorcan both be used as a class which is well documented.

View::composer/View::creator都可以用作一个有据可查的类。

While these give you the ability to pass additional data to a view, they also give you to ability to do a lot of other things, for example they could:

虽然这些使您能够将附加数据传递给视图,但它们也使您能够执行许多其他操作,例如它们可以:

  • Aid in debugging a view
  • Log information about views
  • Be used to create custom caching (probably not a great idea, but possible)
  • 帮助调试视图
  • 记录视图信息
  • 用于创建自定义缓存(可能不是一个好主意,但可能)

These are just some examples of what couldbe possible using View::composerand View::creator.

这些只是什么一些例子可以是可能的使用View::composerView::creator

回答by Atrakeur

View::composer('*', callback());

Means that the callback will be called for all views (*).

意味着将为所有视图 (*) 调用回调。

View::share

Means that a variable will be shared with all outputed views.

意味着一个变量将与所有输出的视图共享。

Because the first is in filters.php, it'll apply for all routes.

因为第一个在filters.php 中,它将适用于所有路由。

The second is in a controller contructor, so it'll apply for all views triggered by this controller.

第二个在控制器构造函数中,因此它将应用于由该控制器触发的所有视图。

One last thing: when overriding a constructor, it's a good pratice to allways call the parent constructor with this code:

最后一件事:在覆盖构造函数时,始终使用以下代码调用父构造函数是一种很好的做法:

parent::_construct();