欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Laravel5.4框架中視圖共享數(shù)據(jù)的方法詳解

 更新時(shí)間:2019年09月05日 10:57:23   作者:張高偉  
這篇文章主要介紹了Laravel5.4框架中視圖共享數(shù)據(jù)的方法,結(jié)合實(shí)例形式詳細(xì)分析了Laravel框架視圖共享數(shù)據(jù)的原理、步驟與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Laravel5.4框架中視圖共享數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

每個(gè)人都會(huì)遇到這種情況:某些數(shù)據(jù)還在每個(gè)頁面進(jìn)行使用,比如用戶信息,或者菜單數(shù)據(jù),最基本的做法是在每個(gè)視圖空控制器中傳入這些數(shù)據(jù),但顯然并不是我們想要的結(jié)果。另一種方法就是使用視圖數(shù)據(jù)共享,視圖數(shù)據(jù)共享的基本使用很簡單,可查看視圖文檔了解詳情,這里我們演示兩個(gè)使用示例:在視圖間共享數(shù)據(jù)和視圖Composer

在視圖中共享數(shù)據(jù)

除了在單個(gè)視圖中傳遞指定數(shù)據(jù)之外,有時(shí)候需要在所有視圖中傳入同一數(shù)據(jù),即我們需要在不同視圖中共享數(shù)據(jù)。要實(shí)現(xiàn)這一目的,需要使用視圖工廠的share方法。

全局幫助函數(shù)view和response類似,如果傳入?yún)?shù),則返回Illuminate\View\View實(shí)例,不傳入?yún)?shù)則返回Illuminate\View\Factory實(shí)例。所以我們可以通過在服務(wù)提供者的boot方法中使用如下方式實(shí)現(xiàn)視圖間共享數(shù)據(jù):

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    //視圖間共享數(shù)據(jù)
    view()->share('name','高偉');
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

接下來我直接用一個(gè)空的Laravel項(xiàng)目做演示,我們在 routes 文件首頁(welcome.blade.php)視圖:

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

然后在視圖中輸出視圖共享數(shù)據(jù):

<!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>

視圖Composer

上面的一種做法雖然可行,但是在別人下載項(xiàng)目后,共享數(shù)據(jù)是從數(shù)據(jù)庫中獲取的,執(zhí)行數(shù)據(jù)遷移的時(shí)候會(huì)報(bào)錯(cuò)。這時(shí)候我們就要用到視圖Composer,視圖Composer通過視圖工廠的composer方法實(shí)現(xiàn)。該方法的第二個(gè)回調(diào)參數(shù)支持基于控制器動(dòng)作和閉包函數(shù)兩種方式。

控制器的動(dòng)作方式

首先要在服務(wù)提供者中注冊視圖 Composer,我們將會(huì)使用輔助函數(shù) view 來訪問 Illuminate\Contracts\View\Factory 的底層實(shí)現(xiàn),記住,Laravel 不會(huì)包含默認(rèn)的視圖 Composer 目錄,我們可以按照自己的喜好組織其路徑,例如可以創(chuàng)建一個(gè) App\Http\ViewComposers 目錄:

<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
/**
* 基于類的實(shí)現(xiàn)方式
*/
class MottoComposer
{
  /**
   * 共享數(shù)據(jù)
   * @date  2018-01-13
   * @author 高偉
   * @param View    $view [description]
   * @return [type]      [description]
   */
  public function compose(View $view)
  {
    $view->with('motto', '嘎嘎嘎,我一直在努力!');
  }
}

然后我們在 AppServiceProvider 中繼續(xù)添加共享數(shù)據(jù):

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    view()->share('name','高偉');
    // 使用基于類的composers...第一個(gè)參數(shù)可以指定共享給那個(gè)視圖,多個(gè)視圖用數(shù)組,共享到全部視圖可以用 *
    view()->composer(
      'welcome', 'App\Http\ViewComposers\MottoComposer'
    );
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

在視圖中顯示共享數(shù)據(jù):

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

閉包實(shí)現(xiàn)方式

閉包的實(shí)現(xiàn)方式相對來說簡單很多,這里就簡單貼書代碼:

<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
  public function boot()
  {
    view()->share('name','高偉');
    // 使用基于類的composers...
    view()->composer(
      'welcome', 'App\Http\ViewComposers\MottoComposer'
    );
    // 閉包實(shí)現(xiàn)方式
    view()->composer('*',function($view)
    {
      $view->with('info','http://www.iwanli.me');
    });
  }
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

視圖中顯示數(shù)據(jù):

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

更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論