這篇文章給大家介紹使用Laravel框架怎么實現多個視圖共享數據,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
傳統方法
假設使用傳統的方法,應該是在每個控制器中都調用數據,然后把數據都塞給視圖。
$menu = DB::table('menu')->get(); return view('xx',['menu'=>$menu]);
稍微優化
新建一個BaseController,然后讓BaseController去獲取數據,然后在每個控制器都繼承BaseController,最后將數據塞到視圖中。
基類
class BaseController{ protected $menu = null;//菜單數據 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]); } }
缺點:在每個控制器中都需要重新設置相同的模板的數據(menu)
最好優化方案
使用Laravel中的View Composers來解決這個問題
1、在App\Providers下創建一個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方法中定義要監聽的視圖,還可以使用通配符,這里我寫的是admin.common.*,如果admin.common.* 下的視圖被渲染的話將會調用App\Http\ViewComposers\AdminComposer@composer 方法
2、注冊ComposerServiceProvider
在config/app.php文件下的providers數組中進行注冊
App\Providers\ComposerServiceProvider::class,
3、創建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 ]);//填充數據 } }
在這里我在構造方法中創建了一個對象,這個對象中包含著數據
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;//消息對象 /** * 構造函數 */ public function __construct(Request $request) { $this->init($request); } /** * 初始化函數 */ private function init(Request $request) { $this->getAdmin($request); $this->getMsg(); $this->getMenu($request); $this->getMbx($request); } /** * 獲取管理員數據 */ private function getAdmin() { $this->admin = session('admin'); } /** * 獲取后臺菜單數據 */ 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); } }
在這里面分別獲取了管理員、菜單、面包屑、消息數據,這些數據都是每個后臺頁面都要使用到的。
注意:這里我將類定義成了CommonUtils,感覺名字取得不好,CommonUtils是存放在App\Libs下的,這個Libs文件夾是我新建的,用于存放工具類的。如果需要給App\Libs文件夾添加自動加載,需要在composer.json文件里做如下修改。
關于使用Laravel框架怎么實現多個視圖共享數據就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。