国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer

Original 2016-12-29 17:40:09 446
abstract:視圖的基本使用很簡(jiǎn)單,可查看視圖文檔了解詳情,今天這里我們演示兩個(gè)使用示例:在視圖間共享數(shù)據(jù)和視圖Composer。下面一起來(lái)看看。1、在視圖間共享數(shù)據(jù)除了在單個(gè)視圖中傳遞指定數(shù)據(jù)之外,有時(shí)候需要在所有視圖中傳入同一數(shù)據(jù),即我們需要在不同視圖中共享數(shù)據(jù)。要實(shí)現(xiàn)這一目的,需要使用視圖工廠的share方法。全局幫助函數(shù)view和response類(lèi)似,如果傳入?yún)?shù),則返回Illuminate\View\

視圖的基本使用很簡(jiǎn)單,可查看視圖文檔了解詳情,今天這里我們演示兩個(gè)使用示例:在視圖間共享數(shù)據(jù)和視圖Composer。下面一起來(lái)看看。

1、在視圖間共享數(shù)據(jù)

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

全局幫助函數(shù)view和response類(lèi)似,如果傳入?yún)?shù),則返回Illuminate\View\View實(shí)例,不傳入?yún)?shù)則返回Illuminate\View\Factory實(shí)例。所以我們可以通過(guò)在服務(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('sitename','Laravel學(xué)院');
  }
 
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}

我們?cè)趓outes.php中定義兩個(gè)路由:

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


然后在resources/views目錄下創(chuàng)建一個(gè)home.blade.php視圖文件,內(nèi)容如下:

{{$sitename}}首頁(yè)

再創(chuàng)建一個(gè)hello.blade.php視圖文件:

歡迎來(lái)到{{$sitename}}!

在瀏覽器中分別訪問(wèn)http://laravel.app:8000/testViewHello和http://laravel.app:8000/testViewHome,則都能解析出$sitename的值。

2、視圖Composer

有時(shí)候我們想要在每次視圖渲染時(shí)綁定一些特定數(shù)據(jù)到視圖中,比如登錄用戶(hù)信息,這時(shí)候我們就要用到視圖Composer,視圖Composer通過(guò)視圖工廠的composer方法實(shí)現(xiàn)。該方法的第二個(gè)回調(diào)參數(shù)支持基于控制器動(dòng)作和閉包函數(shù)兩種方式。

簡(jiǎn)單起見(jiàn),我們還是基于AppServiceProvider,不去單獨(dú)創(chuàng)建服務(wù)提供者,這里我們傳遞閉包參數(shù)(控制器動(dòng)作參考視圖文檔):

<?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('sitename','Laravel學(xué)院');
 
    //視圖Composer
    view()->composer('hello',function($view){
      $view->with('user',array('name'=>'test','avatar'=>'/path/to/test.jpg'));
    });
  }
 
  /**
   * Register any application services.
   *
   * @return void
   */
  public function register()
  {
    //
  }
}


修改hello.blade.php視圖文件:

歡迎來(lái)到{{$sitename}}!
<h3>用戶(hù)信息</h3>
用戶(hù)名:{{$user['name']}}<br>
用戶(hù)頭像:{{$user['avatar']}}

在瀏覽器中訪問(wèn)http://laravel.app:8000/testViewHello,輸出內(nèi)容如下:

歡迎來(lái)到Laravel學(xué)院!

用戶(hù)信息 

用戶(hù)名:test

用戶(hù)頭像:/path/to/test.jpg   

你也可以傳遞數(shù)據(jù)到多個(gè)視圖:

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

甚至所有視圖(使用通配符*):

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

 更多關(guān)于詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer請(qǐng)關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其它文章!  

更多關(guān)于Laravel視圖間共享數(shù)據(jù)與視圖Composer請(qǐng)關(guān)注PHP中文網(wǎng)(m.miracleart.cn)其它文章!


Release Notes

Popular Entries