SoFunction
Updated on 2025-04-09

Detailed explanation of sharing data and view between Laravel views Composer

1. Share data between views

In addition to passing the specified data in a single view, sometimes the same data needs to be passed in all views, i.e. we need to share the data in different views. To achieve this, you need to use the View Factorysharemethod.

Global Help FunctionviewandresponseSimilarly, if the parameter is passed, theIlluminate\View\ViewInstance, if no parameters are passed, returnIlluminate\View\FactoryExample. So we can use the service providerbootThe following method is used to realize sharing data between views:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    //Share data between views    view()->share('sitename','Laravel College');
  }

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

We're inTwo routes are defined:

Route::get('testViewHello',function(){
  return view('hello');
});

Route::get('testViewHome',function(){
  return view('home');
});

Then inresources/viewsCreate a directoryThe view file, the content is as follows:

{{$sitename}}front page

Create another oneView File:

Welcome{{$sitename}}!

Visit separately in the browser:8000/testViewHelloand:8000/testViewHome, all can be parsed out$sitenamevalue.

2. View Composer

Sometimes we want to bind some specific data into the view every time the view is rendered, such as logging in user information. At this time, we have to use the view Composer, which is implemented through the composer method of the view factory. The second callback parameter of this method supports two ways: controller actions and closure functions.

For simplicity, we still based onAppServiceProvider, don't create it separatelyService Provider, here we pass the closure parameters (controller action reference view document):

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    //Share data between views    view()->share('sitename','Laravel College');

    //View Composer    view()->composer('hello',function($view){
      $view->with('user',array('name'=>'test','avatar'=>'/path/to/'));
    });
  }

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

ReviseView File:

Welcome to {{$sitename}}!

<h3>User Information</h3>
username:{{$user['name']}}<br>
User profile picture:{{$user['avatar']}}

Access in a browser:8000/testViewHello, the output content is as follows:

WelcomeLaravelCollege!

User Information

username:test
User profile picture:/path/to/

You can also pass data to multiple views:

view()->composer(['hello','home'],function($view){
  $view->with('user',array('name'=>'test','avatar'=>'/path/to/'));
});

Even all views (using wildcards*):

view()->composer('*',function($view){
  $view->with('user',array('name'=>'test','avatar'=>'/path/to/'));
});

The above is the detailed content of sharing data and view Composer between Laravel views. I hope this article will be helpful to everyone to learn Laravel.