Detailed Explanation of Views Sharing Data in Laravel5.4 Framework

  • 2021-12-21 04:27:55
  • OfStack

This paper illustrates the method of sharing data among views in Laravel 5.4 framework. Share it for your reference, as follows:

Everyone will encounter this situation: some data is still used on every page, such as user information or menu data. The most basic way to pass this data in every view empty controller is obviously not the result we want. Another method is to use view data sharing. The basic use of view data sharing is very simple. You can check the view document for details. Here we demonstrate two examples of use: sharing data between views and view Composer

Sharing data in a view

In addition to passing the specified data in a single view, sometimes we need to pass in the same 1 data in all views, that is, we need to share the data in different views. To achieve this goal, you need to use the share method of view factory.

The global helper function view, similar to response, returns an instance of Illuminate\ View\ View if a parameter is passed in, and an instance of Illuminate\ View\ Factory if no parameter is passed in. Therefore, we can share data between views by using the following methods in the boot method of the service provider:


<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    // Sharing data between views 
    view()->share('name',' Gao Wei ');
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

Next, I will demonstrate directly with an empty Laravel project. We view it on the front page of routes file (welcome. blade. php):


Route::get('/', function () {
  return view('welcome');
});

Then output the view sharing data in the view:


<!DOCTYPE html>
<html>
  <head>
    <title>Laravel</title>
    <style>
      html, body {
        height: 100%;
      }
      body {
        margin: 0;
        padding: 0;
        width: 100%;
        display: table;
        font-weight: 100;
        font-family: 'Lato';
      }
      .container {
        text-align: center;
        display: table-cell;
        vertical-align: middle;
      }
      .content {
        text-align: center;
        display: inline-block;
      }
      .title {
        font-size: 96px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="content">
        <div class="title">Laravel 5</div>
        <h1>hello,{{$name}}</h1>
      </div>
    </div>
  </body>
</html>

View Composer

Although the above method is feasible, after others download the project, the shared data is obtained from the database, and an error will be reported when performing data migration. At this point, we will use the view Composer, which is implemented by the composer method of the view factory. The second callback parameter of this method supports two modes: controller action and closure function.

Operation mode of controller

To first register the view Composer in the service provider, we will use the helper function view to access the underlying implementation of Illuminate\ Contracts\ View\ Factory. Remember that Laravel does not contain the default view Composer directory, and we can organize its path according to our own preferences, for example, we can create an App\ Http\ ViewComposers directory:


<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
/**
*  Class-based implementation 
*/
class MottoComposer
{
  /**
   *  Shared data 
   * @date  2018-01-13
   * @author  Gao Wei 
   * @param View    $view [description]
   * @return [type]      [description]
   */
  public function compose(View $view)
  {
    $view->with('motto', ' Quack quack, I 1 Keep trying! ');
  }
}

Then we continue to add shared data in AppServiceProvider:


<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    view()->share('name',' Gao Wei ');
    //  Using class-based composers... No. 1 1 Parameters can specify which view to share with , Multiple views use arrays, and sharing to all views can be used  *
    view()->composer(
      'welcome', 'App\Http\ViewComposers\MottoComposer'
    );
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

Display shared data in the view:


...
<div class="content">
 <div class="title">Laravel 5</div>
 <h1>hello,{{$name}}</h1>
 <p>{{$motto}}</p>
</div>
...

Closure implementation mode

The implementation of closures is relatively much simpler, so here is a simple book code:


<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    view()->share('name',' Gao Wei ');
    //  Using class-based composers...
    view()->composer(
      'welcome', 'App\Http\ViewComposers\MottoComposer'
    );
    //  Closure implementation mode 
    view()->composer('*',function($view)
    {
      $view->with('info','http://www.iwanli.me');
    });
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

Display data in the view:


...
<div class="content">
 <div class="title">Laravel 5</div>
 <h1>hello,{{$name}}</h1>
 <p>{{$motto}}</p>
 <p>{{$info}}</p>
</div>
...

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: