詳解Laravel視圖間共享數(shù)據(jù)與視圖Composer
1、在視圖間共享數(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('sitename','Laravel學(xué)院'); } /** * Register any application services. * * @return void */ public function register() { // } }
我們?cè)?code>routes.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}}首頁
再創(chuàng)建一個(gè)hello.blade.php
視圖文件:
歡迎來到{{$sitename}}!
在瀏覽器中分別訪問http://laravel.app:8000/testViewHello
和http://laravel.app:8000/testViewHome
,則都能解析出$sitename
的值。
2、視圖Composer
有時(shí)候我們想要在每次視圖渲染時(shí)綁定一些特定數(shù)據(jù)到視圖中,比如登錄用戶信息,這時(shí)候我們就要用到視圖Composer,視圖Composer通過視圖工廠的composer方法實(shí)現(xiàn)。該方法的第二個(gè)回調(diào)參數(shù)支持基于控制器動(dòng)作和閉包函數(shù)兩種方式。
簡單起見,我們還是基于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
視圖文件:
歡迎來到{{$sitename}}!
<h3>用戶信息</h3> 用戶名:{{$user['name']}}<br> 用戶頭像:{{$user['avatar']}}
在瀏覽器中訪問http://laravel.app:8000/testViewHello
,輸出內(nèi)容如下:
歡迎來到Laravel學(xué)院! 用戶信息 用戶名:test 用戶頭像:/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')); });
以上就是Laravel視圖間共享數(shù)據(jù)及視圖Composer的詳細(xì)內(nèi)容,希望本文對(duì)大家學(xué)習(xí)Laravel有所幫助。
相關(guān)文章
PHP中set error handler函數(shù)用法小結(jié)
set_error_handler() 函數(shù)設(shè)置用戶自定義的錯(cuò)誤處理函數(shù)。該函數(shù)用于創(chuàng)建運(yùn)行時(shí)期間的用戶自己的錯(cuò)誤處理方法。該函數(shù)會(huì)返回舊的錯(cuò)誤處理程序,若失敗,則返回 null2015-11-11thinkphp框架表單數(shù)組實(shí)現(xiàn)圖片批量上傳功能示例
這篇文章主要介紹了thinkphp框架表單數(shù)組實(shí)現(xiàn)圖片批量上傳功能,結(jié)合實(shí)例形式分析了js前端圖片上傳與后臺(tái)thinkPHP文件處理相關(guān)操作技巧,需要的朋友可以參考下2020-04-04利用laravel搭建一個(gè)迷你博客實(shí)戰(zhàn)教程
這篇文章主要給大家介紹了關(guān)于利用laravel搭建一個(gè)迷你博客的相關(guān)資料,文中將一步步的實(shí)現(xiàn)步驟通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08php實(shí)現(xiàn)根據(jù)IP地址獲取其所在省市的方法
今天,就討論一下在PHP中如何獲取用戶IP地址、PHP根據(jù)IP判斷用戶所在城市以及PHP根據(jù)IP實(shí)現(xiàn)城市切換或跳轉(zhuǎn)的問題。2015-04-04PHP+iframe圖片上傳實(shí)現(xiàn)即時(shí)刷新效果
下面小編就為大家?guī)硪黄狿HP+iframe圖片上傳實(shí)現(xiàn)即時(shí)刷新效果。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11PHP isset empty函數(shù)相關(guān)面試題及解析
這篇文章主要介紹了PHP isset empty函數(shù)相關(guān)面試題及解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12