視圖
視圖
視圖
創(chuàng)建視圖
{tip} 如果你想找到有關(guān)如何編寫 Blade 模板的更多信息? 從查看完整的 Blade 文檔 入手。
視圖包含應(yīng)用程序的 HTML,并且將控制器 / 應(yīng)用程序邏輯與演示邏輯分開。視圖文件存放于 resources/views
目錄下。一個(gè)簡(jiǎn)單的視圖如下所示:
<!-- 此視圖文件位置 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
幫助器的第一個(gè)參數(shù)對(duì)應(yīng)于 resources/views
目錄中視圖文件的名稱。 第二個(gè)參數(shù)是應(yīng)該可供視圖使用的數(shù)據(jù)數(shù)組。 在這種情況下,我們傳遞 name
變量,該變量使用 Blade syntax 顯示在視圖中。
當(dāng)然,視圖文件也可以嵌套在 resources/views
目錄的子目錄中?!更c(diǎn)」符號(hào)可以用來引用嵌套視圖。例如,如果你的視圖存儲(chǔ)在 resources/views/admin/profile.blade.php
,則可以這樣引用它:
return view('admin.profile', $data);
判斷視圖文件是否存在
如果需要判斷視圖文件是否存在,可以使用 View
facade. 如果視圖文件存在,exists
方法會(huì)返回 true
:
use Illuminate\Support\Facades\View;if (View::exists('emails.customer')) { // }
創(chuàng)建第一個(gè)可用視圖
使用 first
方法,你可以創(chuàng)建存在于給定數(shù)組視圖中的第一個(gè)視圖。 如果你的應(yīng)用程序或開發(fā)的第三方包允許定制或覆蓋視圖,這非常有用:
return view()->first(['custom.admin', 'admin'], $data);
當(dāng)然,你也可以通過 View
facade 調(diào)用這個(gè)方法:
use Illuminate\Support\Facades\View;return View::first(['custom.admin', 'admin'], $data);
向視圖傳遞參數(shù)
正如您在前面的示例中所看到的,您可以將一組數(shù)據(jù)傳遞給視圖:
return view('greetings', ['name' => 'Victoria']);
以這種方式傳遞信息時(shí),數(shù)據(jù)應(yīng)該是具有鍵 / 值對(duì)的數(shù)組。 在視圖中,您可以使用相應(yīng)的鍵訪問每個(gè)值,例如 <?php echo $key; ?>
。 作為將完整的數(shù)據(jù)數(shù)組傳遞給 view
輔助函數(shù)的替代方法,您可以使用 with
方法將各個(gè)數(shù)據(jù)片段添加到視圖中:
return view('greeting')->with('name', 'Victoria');
與所有視圖共享數(shù)據(jù)
如果需要共享一段數(shù)據(jù)給應(yīng)用程序的所有視圖,你可以在服務(wù)提供器的 boot
方法中調(diào)用視圖 Facade 的 share
方法。例如,可以將它們添加到 AppServiceProvider
或者為它們生成一個(gè)單獨(dú)的服務(wù)提供器:
<?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)視圖時(shí)調(diào)用的回調(diào)或類方法。 如果每次呈現(xiàn)視圖時(shí)都希望將數(shù)據(jù)綁定到視圖,則視圖合成器可以幫助您將該邏輯組織到一個(gè)位置。
在下面這個(gè)例子中,我們會(huì)在一個(gè) 服務(wù)提供商 中注冊(cè)視圖合成器。 使用 View
facade 來訪問底層的 Illuminate\Contracts\View\Factory
契約實(shí)現(xiàn)。默認(rè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)建了新的一個(gè)服務(wù)提供器來存放你注冊(cè)視圖合成器的代碼,那么你需要將這個(gè)服務(wù)提供器添加到配置文件
config/app.php
的providers
數(shù)組中。
到此我們已經(jīng)注冊(cè)了視圖合成器,每次渲染 profile
視圖時(shí)都會(huì)執(zhí)行 ProfileComposer@compose
方法。那么下面我們來定義視圖合成器的這個(gè)類吧:
<?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
方法會(huì)在視圖渲染之前被調(diào)用,并傳入一個(gè) Illuminate\View\View
實(shí)例。你可以使用 with
方法將數(shù)據(jù)綁定到視圖。
{tip} 所有的視圖合成器都會(huì)通過 服務(wù)容器 , 進(jìn)行解析,所以你可以在視圖合成器的構(gòu)造函數(shù)中類型提示需要注入的依賴項(xiàng)。
將視圖合成器添加到多個(gè)視圖
通過將一組視圖作為第一個(gè)參數(shù)傳入 composer
方法,將一個(gè)視圖合成器添加到多個(gè)視圖:
View::composer( ['profile', 'dashboard'], 'App\Http\View\Composers\MyViewComposer' );
composer
方法同時(shí)也接受通配符 *
,表示將一個(gè)視圖合成器添加到所有視圖:
View::composer('*', function ($view) { // });
視圖構(gòu)造器
視圖 creators 和視圖合成器非常相似。唯一不同之處在于:視圖構(gòu)造器在視圖實(shí)例化之后立即執(zhí)行,而視圖合成器在視圖即將渲染時(shí)執(zhí)行。使用 creator
方法注冊(cè)視圖構(gòu)造器:
View::creator('profile', 'App\Http\View\Creators\ProfileCreator');