如何将数据传递给 Laravel 5 中的所有视图? [英] How to pass data to all views in Laravel 5?

查看:20
本文介绍了如何将数据传递给 Laravel 5 中的所有视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在我的 Laravel 5 应用程序的所有视图中都可以访问一些默认数据.

I want to have some default data accessible in all views in my Laravel 5 application.

我试图搜索它,但只找到 Laravel 4 的结果.我已阅读文档与所有视图共享数据"在这里,但我不明白该怎么做.下面的代码应该放在哪里?

I have tried to search for it but only find results for Laravel 4. I have read the documentation 'Sharing Data With All Views' here but I can't understand what to do. Where should the following code be placed?

View::share('data', [1, 2, 3]);

感谢您的帮助.

推荐答案

这个目标可以通过不同的方法来实现,

This target can achieve through different method,

1.使用 BaseController

我喜欢的设置方式是,我创建了一个 BaseController 类,它扩展了 Laravel 自己的 Controller,并在那里设置了各种全局事物.所有其他控制器然后从 BaseController 扩展而不是 Laravel 的控制器.

The way I like to set things up, I make a BaseController class that extends Laravel’s own Controller, and set up various global things there. All other controllers then extend from BaseController rather than Laravel’s Controller.

class BaseController extends Controller
{
  public function __construct()
  {
    //its just a dummy data object.
    $user = User::all();

    // Sharing is caring
    View::share('user', $user);
  }
}

2.使用过滤器

如果你知道你想要为整个应用程序中的每个请求的视图设置一些东西,你也可以通过在请求之前运行的过滤器来实现——这就是我在 Laravel 中处理 User 对象的方式.

If you know for a fact that you want something set up for views on every request throughout the entire application, you can also do it via a filter that runs before the request — this is how I deal with the User object in Laravel.

App::before(function($request)
{
  // Set up global user object for views
  View::share('user', User::all());
});

您可以定义自己的过滤器

You can define your own filter

Route::filter('user-filter', function() {
    View::share('user', User::all());
});

并通过简单的过滤器调用来调用它.

and call it through simple filter calling.

根据第 5 版更新.*

3.使用中间件

使用 View::sharemiddleware

Route::group(['middleware' => 'SomeMiddleware'], function(){
  // routes
});



class SomeMiddleware {
  public function handle($request)
  {
    View::share('user', auth()->user());
  }
}

4.使用 View Composer

View Composer 还有助于绑定特定数据以不同方式查看.您可以直接将变量绑定到特定视图或所有视图.例如,您可以根据需要创建自己的目录来存储您的视图作曲家文件.而这些视图 composer 文件通过 Service 提供与视图的交互.

View Composer also help to bind specific data to view in different ways. You can directly bind variable to specific view or to all views. For Example you can create your own directory to store your view composer file according to requirement. and these view composer file through Service provide interact with view.

查看composer方法可以使用不同的方式,第一个例子看起来很像:

View composer method can use different way, First example can look alike:

您可以创建一个 AppHttpViewComposers 目录.

You could create an AppHttpViewComposers directory.

服务提供商

namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer("ViewName","AppHttpViewComposersTestViewComposer");
    }
}

之后,将此提供程序添加到提供程序"部分下的 config/app.php.

After that, add this provider to config/app.php under "providers" section.

TestViewComposer

namespace AppHttpViewComposers;

use IlluminateContractsViewView;

class TestViewComposer {

    public function compose(View $view) {
        $view->with('ViewComposerTestVariable', "Calling with View Composer Provider");
    }
}

ViewName.blade.php

Here you are... {{$ViewComposerTestVariable}}

此方法仅适用于特定视图.但是如果你想对所有视图触发 ViewComposer,我们必须将这个单一的改变应用到 ServiceProvider.

This method could help for only specific View. But if you want trigger ViewComposer to all views, we have to apply this single change to ServiceProvider.

namespace AppProviders;
use IlluminateSupportServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer('*',"AppHttpViewComposersTestViewComposer");
    }
}

参考

Laravel 文档

进一步说明 Laracast Episode

如果我还有不清楚的地方,请告诉我.

If still something unclear from my side, let me know.

这篇关于如何将数据传递给 Laravel 5 中的所有视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆