Laravel 4 - 理解 View::share()

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

Laravel 4 - understanding View::share()

laravellaravel-4

提问by user2094178

From what I understand:

据我了解:

View::share('foo','bar');

Will make $foo available in all views.

将使 $foo 在所有视图中可用。

However, is it correct to say View::share()can be used only in the __construct()?

但是,说View::share()只能在 中使用是否正确__construct()

Because from outside __construct()I can't make it to work.

因为从外面__construct()我不能让它工作。

采纳答案by Jason Lewis

View::shareshould be available anywherewithin your application. A common place that it is used is in view composers, but it should be usable within a route or wherever you need it.

View::share应该可以在您的应用程序中的任何地方使用。使用它的一个常见地方是在视图作曲家中,但它应该可以在路线内或任何您需要的地方使用。

回答by Matt Pavelle

Yes, adding:

是的,补充:

View::share('foo','bar');

in your routes.php file will make $foo (with a value of 'bar') available in all views. This is especially useful for something like Twitter Bootstrap's "active" navigation classes. For example, you could do:

在您的 routes.php 文件中,所有视图中都可以使用 $foo(值为 'bar')。这对于诸如 Twitter Bootstrap 的“活动”导航类之类的东西特别有用。例如,你可以这样做:

View::share('navactive', '');

to make sure the navactive variable is set in all views (and thus won't throw errors) and then when you are making views (in your controller, for example), you could pass:

确保在所有视图中都设置了 navactive 变量(因此不会抛出错误),然后当您创建视图时(例如在您的控制器中),您可以通过:

return View::make('one')->with('navactive', 'one');

and then in your view (preferably some bootstrappy blade template) you can do the following:

然后在您的视图中(最好是一些引导刀片模板),您可以执行以下操作:

<ul class="nav">
    @if ( Auth::user() )
    <li @if ($navactive === 'one') class="active" @endif><a href="{{{ URL::to('one/') }}}">One</a></li>
    <li @if ($navactive === 'three') class="active" @endif><a href="{{{ URL::to('three/') }}}">Three</a></li>
    <li @if ($navactive === 'five') class="active" @endif><a href="{{{ URL::to('five/') }}}">Five</a></li>
    @endif
</ul>

回答by dud3

Basically if you want to share the variables through all view, you might first want to create a base route(E.x.:internalController.php) as a parent class then extend other controllers as a child of it(E.x:childController.php).

基本上,如果您想通过所有视图共享变量,您可能首先要创建一个基本路由(例如:)internalController.php作为父类,然后将其他控制器扩展为它的子类(例如:)childController.php

And yeah you will most likely set the view::share('foo', $bar)in the __constructor()of the internalController.php, since it lunches whenever the class is initialized, this way the parent class will serve the variable values to the child classes.

和是你最有可能设置view::share('foo', $bar)__constructor()internalController.php,因为它的午餐每当类被初始化,这样的父类将成为变量值的子类。