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

視圖

視圖


視圖

創(chuàng)建視圖

{tip} 如果你想找到有關如何編寫 Blade 模板的更多信息? 從查看完整的 Blade 文檔 入手。

視圖包含應用程序的 HTML,并且將控制器 / 應用程序邏輯與演示邏輯分開。視圖文件存放于 resources/views 目錄下。一個簡單的視圖如下所示:

<!-- 此視圖文件位置 resources/views/greeting.blade.php -->
<html>
  <body>
    <h1>Hello, {{ $name }}</h1>    
  </body>
</html>

該視圖文件位于 resources/views/greeting.blade.php,可以使用全局輔助函數(shù) view 來返回:

Route::get('/', function () { 
   return view('greeting', ['name' => 'James']);
});

如您所見,傳遞給 view 幫助器的第一個參數(shù)對應于 resources/views 目錄中視圖文件的名稱。 第二個參數(shù)是應該可供視圖使用的數(shù)據(jù)數(shù)組。 在這種情況下,我們傳遞 name 變量,該變量使用 Blade syntax 顯示在視圖中。

當然,視圖文件也可以嵌套在 resources/views 目錄的子目錄中。「點」符號可以用來引用嵌套視圖。例如,如果你的視圖存儲在 resources/views/admin/profile.blade.php,則可以這樣引用它:

return view('admin.profile', $data);

判斷視圖文件是否存在

如果需要判斷視圖文件是否存在,可以使用 View facade. 如果視圖文件存在,exists 方法會返回  true

use Illuminate\Support\Facades\View;if (View::exists('emails.customer')) { 
   //
}

創(chuàng)建第一個可用視圖

使用 first 方法,你可以創(chuàng)建存在于給定數(shù)組視圖中的第一個視圖。 如果你的應用程序或開發(fā)的第三方包允許定制或覆蓋視圖,這非常有用:

return view()->first(['custom.admin', 'admin'], $data);

當然,你也可以通過 View facade 調用這個方法:

use Illuminate\Support\Facades\View;return View::first(['custom.admin', 'admin'], $data);

向視圖傳遞參數(shù)

正如您在前面的示例中所看到的,您可以將一組數(shù)據(jù)傳遞給視圖:

return view('greetings', ['name' => 'Victoria']);

以這種方式傳遞信息時,數(shù)據(jù)應該是具有鍵 / 值對的數(shù)組。 在視圖中,您可以使用相應的鍵訪問每個值,例如 <?php echo $key; ?>。 作為將完整的數(shù)據(jù)數(shù)組傳遞給 view 輔助函數(shù)的替代方法,您可以使用 with 方法將各個數(shù)據(jù)片段添加到視圖中:

return view('greeting')->with('name', 'Victoria');

與所有視圖共享數(shù)據(jù)

如果需要共享一段數(shù)據(jù)給應用程序的所有視圖,你可以在服務提供器的 boot 方法中調用視圖 Facade 的 share 方法。例如,可以將它們添加到 AppServiceProvider 或者為它們生成一個單獨的服務提供器:

<?php
  namespace App\Providers;
  use Illuminate\Support\Facades\View;
  class AppServiceProvider extends ServiceProvider{   
     /**
     * Bootstrap any application services.
     *
     * @return void
     */   
     public function boot()   
      {      
        View::share('key', 'value');   
       }   
     /**
     * Register the service provider.
     *
     * @return void
     */    
     public function register()  
       {      
         // 
        }
     }

視圖合成器

視圖合成器是在呈現(xiàn)視圖時調用的回調或類方法。 如果每次呈現(xiàn)視圖時都希望將數(shù)據(jù)綁定到視圖,則視圖合成器可以幫助您將該邏輯組織到一個位置。

在下面這個例子中,我們會在一個 服務提供商 中注冊視圖合成器。 使用 View facade 來訪問底層的 Illuminate\Contracts\View\Factory 契約實現(xiàn)。默認情況下,Laravel 沒有存放視圖合成器的目錄,你需要根據(jù)需求來重新建立目錄,例如: app/Http/View/Composers :

<?php
   namespace App\Providers;
   use Illuminate\Support\Facades\View;
   use Illuminate\Support\ServiceProvider;
   class ViewServiceProvider extends ServiceProvider{    
      /**
     * Register bindings in the container.
     *
     * @return void
     */  
    public function boot()  
      {      
        // Using class based composers...        
        View::composer(        
            'profile', 'App\Http\View\Composers\ProfileComposer'     
          );      
        // Using Closure based composers...        
        View::composer('dashboard', function ($view) {      
              //  
           });    
        }   
    /**
     * Register the service provider.
     *
     * @return void
     */   
     public function register()  
       {   
            //   
        }
      }

{note} 注意,如果你創(chuàng)建了新的一個服務提供器來存放你注冊視圖合成器的代碼,那么你需要將這個服務提供器添加到配置文件 config/app.phpproviders 數(shù)組中。

到此我們已經注冊了視圖合成器,每次渲染 profile 視圖時都會執(zhí)行 ProfileComposer@compose 方法。那么下面我們來定義視圖合成器的這個類吧:

<?php
   namespace App\Http\View\Composers;
   use Illuminate\View\View;
   use App\Repositories\UserRepository;
   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());   
       }
    }

視圖合成器的 compose 方法會在視圖渲染之前被調用,并傳入一個 Illuminate\View\View 實例。你可以使用 with 方法將數(shù)據(jù)綁定到視圖。

{tip} 所有的視圖合成器都會通過 服務容器 ,  進行解析,所以你可以在視圖合成器的構造函數(shù)中類型提示需要注入的依賴項。

將視圖合成器添加到多個視圖

通過將一組視圖作為第一個參數(shù)傳入 composer 方法,將一個視圖合成器添加到多個視圖:

View::composer( 
   ['profile', 'dashboard'],    
   'App\Http\View\Composers\MyViewComposer'
  );

composer 方法同時也接受通配符 * ,表示將一個視圖合成器添加到所有視圖:

View::composer('*', function ($view) {  
   //
});

視圖構造器

視圖 creators 和視圖合成器非常相似。唯一不同之處在于:視圖構造器在視圖實例化之后立即執(zhí)行,而視圖合成器在視圖即將渲染時執(zhí)行。使用 creator 方法注冊視圖構造器:

View::creator('profile', 'App\Http\View\Creators\ProfileCreator');
本文章首發(fā)在 LearnKu.com 網站上。