如何将数据传递给Laravel 5中的所有视图?

时间:2021-07-23 18:27:55

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

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

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?

我试着去搜索它,但是只找到了Laravel 4的搜索结果。我在这里读过“与所有视图共享数据”的文档,但我不知道该怎么做。下面的代码应该放在哪里?

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

Thanks for your help.

谢谢你的帮助。

10 个解决方案

#1


125  

This target can achieve through different method,

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

1. Using BaseController

1。使用BaseController

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.

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

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

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

2. Using Filter

2。使用过滤器

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.

如果您知道您希望在整个应用程序中对每个请求都设置视图,那么您也可以通过在请求之前运行的过滤器来完成它——这就是我如何处理Laravel中的User对象。

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

OR

You can define your own filter

您可以定义自己的过滤器

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

and call it through simple filter calling.

通过简单的过滤器调用。

Update According to Version 5.*

根据版本5进行更新*

3. Using View Composer

3所示。使用视图的作曲家

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.

视图编写器还帮助以不同的方式将特定数据绑定到视图。您可以直接将变量绑定到特定的视图或所有的视图。例如,您可以根据需要创建自己的目录来存储视图编写器文件。这些视图编写器文件通过服务提供与视图的交互。

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

View composer方法可以用不同的方式,第一个例子可以看起来一样:

You could create an App\Http\ViewComposers directory.

您可以创建一个App\Http\ viewcomposer目录。

Service Provider

服务提供者

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
    }
}

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

然后,将这个提供程序添加到配置/应用程序中。php在“供应商”一节。

TestViewComposer

TestViewComposer

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;

class TestViewComposer {

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

ViewName.blade.php

ViewName.blade.php

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

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.

这种方法只能帮助特定的视图。但是,如果您想要将触发器ViewComposer应用到所有的视图中,我们必须将这个单独的更改应用到ServiceProvider中。

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
    }
}

Reference

参考

Laravel Documentation

Laravel文档

For Further Clarification Laracast Episode

为了进一步澄清Laracast事件

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

如果我这边还有什么不清楚的地方,请告诉我。

#2


44  

You can either create your own service provider (ViewServiceProvider name is common) or you can use the existing AppServiceProvider.

您可以创建自己的服务提供者(ViewServiceProvider名称是通用的),也可以使用现有的AppServiceProvider。

In your selected provider, put your code in the boot method.

在选定的提供程序中,将代码放在boot方法中。

public function boot() {
    view()->share('data', [1, 2, 3]);
}

This will make a $data variable accessible in all your views.

这将使$data变量可在所有视图中访问。

If you rather want to use the facade instead of the helper, change view()-> to View:: but don't forget to have use View; at the top of your file.

如果您希望使用facade而不是helper,请更改view()->以查看:::但不要忘记拥有use view;在文件的顶部。

#3


7  

I found this to be the easiest one. Create a new provider and user the '*' wildcard to attach it to all views. Works in 5.3 as well :-)

我发现这是最简单的一个。创建一个新的提供者并使用“*”通配符将其附加到所有视图。作品还有5.3:-)

<?php

namespace App\Providers;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     * @return void
     */
    public function boot()
    {
        view()->composer('*', function ($view)
        {
            $user = request()->user();

            $view->with('user', $user);
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

#4


2  

The best way would be sharing the variable using View::share('var', $value);

最好的方法是使用View: share('var', $value)共享变量;

Problems with composing using "*":

使用“*”写作存在的问题:

Consider following approach:

考虑以下方法:

<?php
// from AppServiceProvider::boot()
$viewFactory = $this->app->make(Factory::class);

$viewFacrory->compose('*', GlobalComposer::class);

From an example blade view:

从一个示例blade视图:

  @for($i = 0; $i<1000; $i++)
    @include('some_partial_view_to_display_i', ['toDisplay' => $i])
  @endfor

What happens?

会发生什么呢?

  • The GlobalComposer class is instantiated 1000 times using App::make.
  • 使用App::make实例化GlobalComposer类1000次。
  • The event composing:some_partial_view_to_display_i is handled 1000 times.
  • 事件组成:some_partial_view_to_display_i被处理1000次。
  • The compose function inside the GlobalComposer class is called 1000 times.
  • GlobalComposer类内部的组合函数称为1000次。

But the partial view some_partial_view_to_display_i has nothing to do with the variables composed by GlobalComposer but heavily increases render time.

但是局部视图some_partial_view_to_display_i与GlobalComposer所组成的变量没有关系,但极大地增加了渲染时间。

Best approach?

最好的方法?

Using View::share along a grouped middleware.

使用视图::共享分组的中间件。

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

Route::group(['prefix' => 'api'], function (){

});

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

Update

更新

If you are using something that is computed over the middleware pipeline you can simply listen to the proper event or put the view share middleware at the last bottom of the pipeline.

如果您正在使用在中间件管道上计算的东西,您可以简单地侦听适当的事件,或者将视图共享中间件放在管道的最后一层。

#5


2  

The documentation is hear https://laravel.com/docs/5.4/views#view-composers but i will break it down

文档是听https://laravel.com/docs/5.4/ view# view-composer的,但我将分解它

  1. Look for the directory app\Providers in the root directory of your application and create the file ComposerServiceProvider.php and copy and past the text below into it and save it.

    在应用程序的根目录中查找目录应用程序\提供程序,并创建文件ComposerServiceProvider。把下面的文本复制并保存到php中。

    <?php
        namespace App\Providers;
        use Illuminate\Support\Facades\View;
        use Illuminate\Support\ServiceProvider;
    
        class ComposerServiceProvider extends ServiceProvider
        {
            /**
            * Register bindings in the container.
            *
            * @return void
            */
        public function boot()
        {
            // Using class based composers...
            View::composer(
                'profile', 'App\Http\ViewComposers\ProfileComposer'
            );
    
            // Using Closure based composers...
            View::composer('dashboard', function ($view) {
                //
            });
        }
    
        /**
        * Register the service provider.
        *
        * @return void
        */
        public function register()
        {
            //
        }
    }
    
  2. From the root of your application open Config/app.php and look for the Providers section in the file and copy and past this 'App\Providers\ComposerServiceProvider', to the array.

    从应用程序的根打开配置/应用程序。php并在文件中查找provider部分,并将这个“App\ provider \ComposerServiceProvider”复制到数组中。

By doing this, we have created the Composer Service Provider. When you run your application with the view Profile like so http://yourdomain/something/profile, the service provider ComposerServiceProvider is called and the class App\Http\ViewComposers\ProfileComposer is instantiated calling the method Composer due to the code below inside the boot method or function.

通过这样做,我们创建了Composer服务提供者。当您使用类似于so http://yourdomain/something/profile的视图配置文件运行应用程序时,将调用服务提供程序ComposerServiceProvider,并实例化类应用程序\Http\ viewcomposer分析器ProfileComposer调用方法编写器,因为引导方法或函数中的下面的代码。

 // Using class based composers...
 View::composer(
   'profile', 'App\Http\ViewComposers\ProfileComposer'
 );
  1. If you refresh your application you will get an error because the class App\Http\ViewComposers\ProfileComposer does not exist yet. Now lets create it.
  2. 如果您刷新应用程序,您将会得到一个错误,因为类应用程序\Http\ viewcomposer \ProfileComposer还不存在。现在让我们创建它。

Go to the directory path app/Http

转到目录路径应用程序/Http

  • Create the directory called ViewComposers

    创建名为viewcomposer的目录

  • Create the file ProfileComposer.php.

    ProfileComposer.php创建的文件。

    class ProfileComposer
    {
        /**
        * The user repository implementation.
        *
        * @var UserRepository
        */
        protected $users;
    
        /**
        * Create a new profile composer.
        *
        * @param  UserRepository  $users
        * @return void
        */
        public function __construct(UserRepository $users)
        {
            // Dependencies automatically resolved by service container...
            $this->users = $users;
        }
    
        /**
        * Bind data to the view.
        *
        * @param  View  $view
        * @return void
        */
        public function compose(View $view)
        {
            $view->with('count', $this->users->count());
        }
    }
    

Now go to your view or in this case Profile.blade.php and add

现在转到你的视图或者在这种情况下,Profile.blade。php和添加

{{ $count }}

and that will show the count of users on the profile page.

这将显示配置文件页面上的用户数量。

To show the count on all pages change

显示所有页面的计数改变

// Using class based composers...
View::composer(
    'profile', 'App\Http\ViewComposers\ProfileComposer'
);

To

// Using class based composers...
View::composer(
    '*', 'App\Http\ViewComposers\ProfileComposer'
);

#6


1  

In the documentation:

在文档:

Typically, you would place calls to the share method within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them.

通常,您会在服务提供者的引导方法中调用共享方法。您可以将它们添加到AppServiceProvider或者生成一个单独的服务提供者来存放它们。

I'm agree with Marwelln, just put it in AppServiceProvider in the boot function:

我同意Marwelln的观点,把它放在AppServiceProvider的boot函数中:

public function boot() {
    View::share('youVarName', [1, 2, 3]);
}

I recommend use an specific name for the variable, to avoid confussions or mistakes with other no 'global' variables.

我建议为变量使用一个特定的名称,以避免与其他“全局”变量混淆或错误。

#7


0  

I think that the best way is with View Composers. If someone came here and want to find how can do it with View Composers way, read my answer => How to share a variable across all views?

我认为最好的方法是与视图作曲家。如果有人来到这里想要找到如何用视图作曲家的方式来做,请阅读我的答案=>如何在所有视图*享一个变量?

#8


0  

The documentation is hear https://laravel.com/docs/5.4/views#view-composers but i will break it down 1.Look for the directory Providers in your root directory and create the for ComposerServiceProvider.php with content

文档是听https://laravel.com/docs/5.4/ view# view-composer,但我将把它分解为1。在根目录中查找目录提供程序,并创建用于组合服务提供者。php与内容

#9


0  

Inside your config folder you can create a php file name it for example "variable.php" with content below:

在配置文件夹中,您可以创建一个php文件名,例如“变量”。php”,内容如下:

<?php return [ 'versionNumber' => '122231', ];

< ?php返回['versionNumber' => '122231'];

Now inside all the views you can use config('variable.versionNumber') to call this variable.

现在,在所有视图中,可以使用config('variable.versionNumber')调用此变量。

#10


0  

add them to App service provider in boot method

将它们添加到引导方法中的应用程序服务提供者。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('signedIn', \Auth::check());

     View::share('user', \Auth::user());
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

#1


125  

This target can achieve through different method,

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

1. Using BaseController

1。使用BaseController

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.

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

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

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

2. Using Filter

2。使用过滤器

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.

如果您知道您希望在整个应用程序中对每个请求都设置视图,那么您也可以通过在请求之前运行的过滤器来完成它——这就是我如何处理Laravel中的User对象。

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

OR

You can define your own filter

您可以定义自己的过滤器

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

and call it through simple filter calling.

通过简单的过滤器调用。

Update According to Version 5.*

根据版本5进行更新*

3. Using View Composer

3所示。使用视图的作曲家

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.

视图编写器还帮助以不同的方式将特定数据绑定到视图。您可以直接将变量绑定到特定的视图或所有的视图。例如,您可以根据需要创建自己的目录来存储视图编写器文件。这些视图编写器文件通过服务提供与视图的交互。

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

View composer方法可以用不同的方式,第一个例子可以看起来一样:

You could create an App\Http\ViewComposers directory.

您可以创建一个App\Http\ viewcomposer目录。

Service Provider

服务提供者

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer("ViewName","App\Http\ViewComposers\TestViewComposer");
    }
}

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

然后,将这个提供程序添加到配置/应用程序中。php在“供应商”一节。

TestViewComposer

TestViewComposer

namespace App\Http\ViewComposers;

use Illuminate\Contracts\View\View;

class TestViewComposer {

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

ViewName.blade.php

ViewName.blade.php

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

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.

这种方法只能帮助特定的视图。但是,如果您想要将触发器ViewComposer应用到所有的视图中,我们必须将这个单独的更改应用到ServiceProvider中。

namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider {
    public function boot() {
        view()->composer('*',"App\Http\ViewComposers\TestViewComposer");
    }
}

Reference

参考

Laravel Documentation

Laravel文档

For Further Clarification Laracast Episode

为了进一步澄清Laracast事件

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

如果我这边还有什么不清楚的地方,请告诉我。

#2


44  

You can either create your own service provider (ViewServiceProvider name is common) or you can use the existing AppServiceProvider.

您可以创建自己的服务提供者(ViewServiceProvider名称是通用的),也可以使用现有的AppServiceProvider。

In your selected provider, put your code in the boot method.

在选定的提供程序中,将代码放在boot方法中。

public function boot() {
    view()->share('data', [1, 2, 3]);
}

This will make a $data variable accessible in all your views.

这将使$data变量可在所有视图中访问。

If you rather want to use the facade instead of the helper, change view()-> to View:: but don't forget to have use View; at the top of your file.

如果您希望使用facade而不是helper,请更改view()->以查看:::但不要忘记拥有use view;在文件的顶部。

#3


7  

I found this to be the easiest one. Create a new provider and user the '*' wildcard to attach it to all views. Works in 5.3 as well :-)

我发现这是最简单的一个。创建一个新的提供者并使用“*”通配符将其附加到所有视图。作品还有5.3:-)

<?php

namespace App\Providers;

use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;

class ViewServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     * @return void
     */
    public function boot()
    {
        view()->composer('*', function ($view)
        {
            $user = request()->user();

            $view->with('user', $user);
        });
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

#4


2  

The best way would be sharing the variable using View::share('var', $value);

最好的方法是使用View: share('var', $value)共享变量;

Problems with composing using "*":

使用“*”写作存在的问题:

Consider following approach:

考虑以下方法:

<?php
// from AppServiceProvider::boot()
$viewFactory = $this->app->make(Factory::class);

$viewFacrory->compose('*', GlobalComposer::class);

From an example blade view:

从一个示例blade视图:

  @for($i = 0; $i<1000; $i++)
    @include('some_partial_view_to_display_i', ['toDisplay' => $i])
  @endfor

What happens?

会发生什么呢?

  • The GlobalComposer class is instantiated 1000 times using App::make.
  • 使用App::make实例化GlobalComposer类1000次。
  • The event composing:some_partial_view_to_display_i is handled 1000 times.
  • 事件组成:some_partial_view_to_display_i被处理1000次。
  • The compose function inside the GlobalComposer class is called 1000 times.
  • GlobalComposer类内部的组合函数称为1000次。

But the partial view some_partial_view_to_display_i has nothing to do with the variables composed by GlobalComposer but heavily increases render time.

但是局部视图some_partial_view_to_display_i与GlobalComposer所组成的变量没有关系,但极大地增加了渲染时间。

Best approach?

最好的方法?

Using View::share along a grouped middleware.

使用视图::共享分组的中间件。

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

Route::group(['prefix' => 'api'], function (){

});

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

Update

更新

If you are using something that is computed over the middleware pipeline you can simply listen to the proper event or put the view share middleware at the last bottom of the pipeline.

如果您正在使用在中间件管道上计算的东西,您可以简单地侦听适当的事件,或者将视图共享中间件放在管道的最后一层。

#5


2  

The documentation is hear https://laravel.com/docs/5.4/views#view-composers but i will break it down

文档是听https://laravel.com/docs/5.4/ view# view-composer的,但我将分解它

  1. Look for the directory app\Providers in the root directory of your application and create the file ComposerServiceProvider.php and copy and past the text below into it and save it.

    在应用程序的根目录中查找目录应用程序\提供程序,并创建文件ComposerServiceProvider。把下面的文本复制并保存到php中。

    <?php
        namespace App\Providers;
        use Illuminate\Support\Facades\View;
        use Illuminate\Support\ServiceProvider;
    
        class ComposerServiceProvider extends ServiceProvider
        {
            /**
            * Register bindings in the container.
            *
            * @return void
            */
        public function boot()
        {
            // Using class based composers...
            View::composer(
                'profile', 'App\Http\ViewComposers\ProfileComposer'
            );
    
            // Using Closure based composers...
            View::composer('dashboard', function ($view) {
                //
            });
        }
    
        /**
        * Register the service provider.
        *
        * @return void
        */
        public function register()
        {
            //
        }
    }
    
  2. From the root of your application open Config/app.php and look for the Providers section in the file and copy and past this 'App\Providers\ComposerServiceProvider', to the array.

    从应用程序的根打开配置/应用程序。php并在文件中查找provider部分,并将这个“App\ provider \ComposerServiceProvider”复制到数组中。

By doing this, we have created the Composer Service Provider. When you run your application with the view Profile like so http://yourdomain/something/profile, the service provider ComposerServiceProvider is called and the class App\Http\ViewComposers\ProfileComposer is instantiated calling the method Composer due to the code below inside the boot method or function.

通过这样做,我们创建了Composer服务提供者。当您使用类似于so http://yourdomain/something/profile的视图配置文件运行应用程序时,将调用服务提供程序ComposerServiceProvider,并实例化类应用程序\Http\ viewcomposer分析器ProfileComposer调用方法编写器,因为引导方法或函数中的下面的代码。

 // Using class based composers...
 View::composer(
   'profile', 'App\Http\ViewComposers\ProfileComposer'
 );
  1. If you refresh your application you will get an error because the class App\Http\ViewComposers\ProfileComposer does not exist yet. Now lets create it.
  2. 如果您刷新应用程序,您将会得到一个错误,因为类应用程序\Http\ viewcomposer \ProfileComposer还不存在。现在让我们创建它。

Go to the directory path app/Http

转到目录路径应用程序/Http

  • Create the directory called ViewComposers

    创建名为viewcomposer的目录

  • Create the file ProfileComposer.php.

    ProfileComposer.php创建的文件。

    class ProfileComposer
    {
        /**
        * The user repository implementation.
        *
        * @var UserRepository
        */
        protected $users;
    
        /**
        * Create a new profile composer.
        *
        * @param  UserRepository  $users
        * @return void
        */
        public function __construct(UserRepository $users)
        {
            // Dependencies automatically resolved by service container...
            $this->users = $users;
        }
    
        /**
        * Bind data to the view.
        *
        * @param  View  $view
        * @return void
        */
        public function compose(View $view)
        {
            $view->with('count', $this->users->count());
        }
    }
    

Now go to your view or in this case Profile.blade.php and add

现在转到你的视图或者在这种情况下,Profile.blade。php和添加

{{ $count }}

and that will show the count of users on the profile page.

这将显示配置文件页面上的用户数量。

To show the count on all pages change

显示所有页面的计数改变

// Using class based composers...
View::composer(
    'profile', 'App\Http\ViewComposers\ProfileComposer'
);

To

// Using class based composers...
View::composer(
    '*', 'App\Http\ViewComposers\ProfileComposer'
);

#6


1  

In the documentation:

在文档:

Typically, you would place calls to the share method within a service provider's boot method. You are free to add them to the AppServiceProvider or generate a separate service provider to house them.

通常,您会在服务提供者的引导方法中调用共享方法。您可以将它们添加到AppServiceProvider或者生成一个单独的服务提供者来存放它们。

I'm agree with Marwelln, just put it in AppServiceProvider in the boot function:

我同意Marwelln的观点,把它放在AppServiceProvider的boot函数中:

public function boot() {
    View::share('youVarName', [1, 2, 3]);
}

I recommend use an specific name for the variable, to avoid confussions or mistakes with other no 'global' variables.

我建议为变量使用一个特定的名称,以避免与其他“全局”变量混淆或错误。

#7


0  

I think that the best way is with View Composers. If someone came here and want to find how can do it with View Composers way, read my answer => How to share a variable across all views?

我认为最好的方法是与视图作曲家。如果有人来到这里想要找到如何用视图作曲家的方式来做,请阅读我的答案=>如何在所有视图*享一个变量?

#8


0  

The documentation is hear https://laravel.com/docs/5.4/views#view-composers but i will break it down 1.Look for the directory Providers in your root directory and create the for ComposerServiceProvider.php with content

文档是听https://laravel.com/docs/5.4/ view# view-composer,但我将把它分解为1。在根目录中查找目录提供程序,并创建用于组合服务提供者。php与内容

#9


0  

Inside your config folder you can create a php file name it for example "variable.php" with content below:

在配置文件夹中,您可以创建一个php文件名,例如“变量”。php”,内容如下:

<?php return [ 'versionNumber' => '122231', ];

< ?php返回['versionNumber' => '122231'];

Now inside all the views you can use config('variable.versionNumber') to call this variable.

现在,在所有视图中,可以使用config('variable.versionNumber')调用此变量。

#10


0  

add them to App service provider in boot method

将它们添加到引导方法中的应用程序服务提供者。

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('signedIn', \Auth::check());

     View::share('user', \Auth::user());
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}