您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用Laravel框架怎么實現(xiàn)多個視圖共享數(shù)據(jù),內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
傳統(tǒng)方法
假設使用傳統(tǒng)的方法,應該是在每個控制器中都調(diào)用數(shù)據(jù),然后把數(shù)據(jù)都塞給視圖。
$menu = DB::table('menu')->get(); return view('xx',['menu'=>$menu]);
稍微優(yōu)化
新建一個BaseController,然后讓BaseController去獲取數(shù)據(jù),然后在每個控制器都繼承BaseController,最后將數(shù)據(jù)塞到視圖中。
基類
class BaseController{ protected $menu = null;//菜單數(shù)據(jù) public function __construct(){ $this->getMenu();//獲取導航菜單 } public function getMenu(){ $this->menu = DB::table('menu')->get(); } }
A控制器
class AController extends BaseController{ public function index(){ return view('admin.index',['menu'=>$this->menu,'user'=>$user]); } }
缺點:在每個控制器中都需要重新設置相同的模板的數(shù)據(jù)(menu)
最好優(yōu)化方案
使用Laravel中的View Composers來解決這個問題
1、在App\Providers下創(chuàng)建一個ComposerServiceProvider類
<?php namespace App\Providers; use Illuminate\Support\Facades\View; use Illuminate\Support\ServiceProvider; class ComposerServiceProvider extends ServiceProvider { /** * Register bindings in the container. * * @return void */ public function boot() { // 基于類的view composer View::composer( 'admin.common.*', 'App\Http\ViewComposers\AdminComposer' ); } /** * Register the service provider. * * @return void */ public function register() { // } }
在boot方法中定義要監(jiān)聽的視圖,還可以使用通配符,這里我寫的是admin.common.*,如果admin.common.* 下的視圖被渲染的話將會調(diào)用App\Http\ViewComposers\AdminComposer@composer 方法
2、注冊ComposerServiceProvider
在config/app.php文件下的providers數(shù)組中進行注冊
App\Providers\ComposerServiceProvider::class,
3、創(chuàng)建AdminComposer類
Laravel推薦把view composer類放在app\Http\ViewComposers目錄下,這個目錄一開始是沒有的,需要新建
<?php namespace App\Http\ViewComposers; use App\Libs\CommonUtils; use Illuminate\Http\Request; use Illuminate\View\View; class AdminComposer { private $data = null;//CommonUtils對象 public function __construct(Request $request) { $this->data = new CommonUtils($request);//新建一個CommonUtils對象 } public function compose(View $view) { $view->with([ 'admin' => $this->data->admin, 'mbx' => $this->data->mbx, 'menu' => $this->data->menu, 'msg' => $this->data->msg ]);//填充數(shù)據(jù) } }
在這里我在構(gòu)造方法中創(chuàng)建了一個對象,這個對象中包含著數(shù)據(jù)
5、CommonUtils文件
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/4/20 0020 * Time: 19:49 */ namespace App\Libs; use App\Admin; use App\Perm; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class CommonUtils { public $admin = null;//管理員對象 public $menu = null;//菜單對象 public $mbx = null;//面包屑對象 public $msg = null;//消息對象 /** * 構(gòu)造函數(shù) */ public function __construct(Request $request) { $this->init($request); } /** * 初始化函數(shù) */ private function init(Request $request) { $this->getAdmin($request); $this->getMsg(); $this->getMenu($request); $this->getMbx($request); } /** * 獲取管理員數(shù)據(jù) */ private function getAdmin() { $this->admin = session('admin'); } /** * 獲取后臺菜單數(shù)據(jù) */ private function getMenu(Request $request) { $menu = DB::table('menu')->where('parentid', 0)->orderBy('sort')->get(); $router = $request->getPathInfo(); $perm = new Perm(); $mbx = $perm->getMbx($router); foreach ($menu as $k => $m) { $m->active = ''; //讀取子菜單 $childMenu = DB::table('menu')->where('parentid', $m->id)->orderBy('sort')->get(); if (count($childMenu) > 0) { foreach($childMenu as $v){ $v->active = ''; if($mbx[0]->router == $v->router){ $v->active = 'active'; $m->active = 'active'; } } $m->childMenu = $childMenu; } else { $m->childMenu = null; } } $this->menu = $menu; } /** * 獲取面包屑 */ private function getMbx(Request $request) { $router = $request->getPathInfo(); $perm = new Perm(); $mbx = $perm->getMbx($router); $this->mbx = $mbx; } /** * 獲取未讀消息 */ private function getMsg() { $adminModel = new Admin(); $toId = $this->admin->id; $this->msg = $adminModel->getUnReadMsg($toId); } }
在這里面分別獲取了管理員、菜單、面包屑、消息數(shù)據(jù),這些數(shù)據(jù)都是每個后臺頁面都要使用到的。
注意:這里我將類定義成了CommonUtils,感覺名字取得不好,CommonUtils是存放在App\Libs下的,這個Libs文件夾是我新建的,用于存放工具類的。如果需要給App\Libs文件夾添加自動加載,需要在composer.json文件里做如下修改。
關(guān)于使用Laravel框架怎么實現(xiàn)多個視圖共享數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。