[英]Laravel 5: View loaded event
我要聽的View
負載(或呈現...)事件,我該怎么辦,在Laravel 5使用*Event::
門面在routes.php
文件。
例如,如果要在每次加載視圖時傳遞一些公共數據,則可以使用view :: composer ,在這種情況下,請在“ App \\ Http \\ ViewComposers”中創建一個view::composer
並使用服務進行注冊像這樣的提供者:
<?php namespace App\Providers;
use View;
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider {
public function boot()
{
// Run "compose" method from "App\Http\ViewComposers\ProfileComposer" class
// whenever the "profile" view (Basically profile.blade.php) view is loaded
View::composer('profile', 'App\Http\ViewComposers\ProfileComposer');
}
}
然后創建ProfileComposer
這樣的(來自Laravel
文檔):
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Users\Repository as UserRepository;
class ProfileComposer {
protected $users;
public function __construct(UserRepository $users)
{
$this->users = $users;
}
// Bind data to the view
public function compose(View $view)
{
$view->with('count', $this->users->count());
}
}
因此,每次加載profile view
, $count
變量都會綁定到該視圖中,並且可以像在view
其他變量一樣使用它。 而已。 在Laravel網站上閱讀更多內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.