在公用模板的继承中,如果一旦使用数据库的数据来渲染导航和菜单,那么就在每次调用该模板时的控制器方法中提供数据查询代码,这样,代码也是相当臃肿。为了解决这个问题,laravel框架为我们准备了视图合成器。
<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" type="text/css" href="/static/lib/jquery/jquery.min.js" />
<link rel="Bookmark" href="favicon.ico" >
<link rel="Shortcut Icon" href="favicon.ico" />
<!--[if lt IE 9]>
<script type="text/javascript" src="/static/lib/html5.js"></script>
<script type="text/javascript" src="/static/lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="/static/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="/static/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="/static/lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="/static/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="/static/static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="http://lib.h-ui.net/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<!--/meta 作为公共模版分离出去-->
<title>@yield('title')</title>
<meta name="keywords" content="H-ui.admin v3.0,H-ui网站后台模版,后台模版下载,后台管理系统模版,HTML后台模版下载">
<meta name="description" content="H-ui.admin v3.0,是一款由国人开发的轻量级扁平化网站后台模板,完全免费开源的网站后台管理系统模版,适合中小型CMS后台系统。">
</head>
<body>
<!--_header 作为公共模版分离出去-->
<header class="navbar-wrapper">
<div class="navbar navbar-fixed-top">
<div class="container-fluid cl"> <a class="logo navbar-logo f-l mr-10 hidden-xs" href="/aboutHui.shtml">易住管理系统</a> <a class="logo navbar-logo-m f-l mr-10 visible-xs" href="/aboutHui.shtml">H-ui</a>
<span class="logo navbar-slogan f-l mr-10 hidden-xs">v3.0</span>
<a aria-hidden="false" class="nav-toggle Hui-iconfont visible-xs" href="javascript:;"></a>
<nav class="nav navbar-nav">
<ul class="cl">
<li class="dropDown dropDown_hover"><a href="javascript:;" class="dropDown_A"><i class="Hui-iconfont"></i> 新增 <i class="Hui-iconfont"></i></a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="javascript:;" onclick="article_add('添加资讯','article-add.html')"><i class="Hui-iconfont"></i> 资讯</a></li>
<li><a href="javascript:;" onclick="picture_add('添加资讯','picture-add.html')"><i class="Hui-iconfont"></i> 图片</a></li>
<li><a href="javascript:;" onclick="product_add('添加资讯','product-add.html')"><i class="Hui-iconfont"></i> 产品</a></li>
<li><a href="javascript:;" onclick="member_add('添加用户','member-add.html','','510')"><i class="Hui-iconfont"></i> 用户</a></li>
</ul>
</li>
</ul>
</nav>
<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">
<ul class="cl">
<li>{{$group['title']}}</li>
<li class="dropDown dropDown_hover"> <a href="#" class="dropDown_A">{{$admin['username']}} <i class="Hui-iconfont"></i></a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="javascript:;" onClick="myselfinfo()">个人信息</a></li>
<li><a href="#">切换账户</a></li>
<li><a href="#">退出</a></li>
</ul>
</li>
<li id="Hui-msg"> <a href="#" title="消息"><span class="badge badge-danger">1</span><i class="Hui-iconfont" style="font-size:18px"></i></a> </li>
<li id="Hui-skin" class="dropDown right dropDown_hover"> <a href="javascript:;" class="dropDown_A" title="换肤"><i class="Hui-iconfont" style="font-size:18px"></i></a>
<ul class="dropDown-menu menu radius box-shadow">
<li><a href="javascript:;" data-val="default" title="默认(黑色)">默认(黑色)</a></li>
<li><a href="javascript:;" data-val="blue" title="蓝色">蓝色</a></li>
<li><a href="javascript:;" data-val="green" title="绿色">绿色</a></li>
<li><a href="javascript:;" data-val="red" title="红色">红色</a></li>
<li><a href="javascript:;" data-val="yellow" title="黄色">黄色</a></li>
<li><a href="javascript:;" data-val="orange" title="橙色">橙色</a></li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
</header>
<!--/_header 作为公共模版分离出去-->
<!--_menu 作为公共模版分离出去-->
** <aside class="Hui-aside">
<div class="menu_dropdown bk_2">
@foreach($menus as $menu)
<dl id="menu-member">
<dt><i class="Hui-iconfont"></i> {{$menu['title']}}<i class="Hui-iconfont menu_dropdown-arrow"></i></dt>
@if(isset($menu['children']))
<dd>
@foreach($menu['children'] as $child)
<ul>
<li><a href="/admins/{{$child['controller'].'/'.$child['action']}}" title="{{$child['title']}}">{{$child['title']}}</a></li>
</ul>
@endforeach
</dd>
@endif
</dl>
@endforeach
</div>
</aside>**
<div class="dislpayArrow hidden-xs"><a class="pngfix" href="javascript:void(0);" onClick="displaynavbar(this)"></a></div>
<!--/_menu 作为公共模版分离出去-->
@yield('content')
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="/static/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/static/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/static/static/h-ui/js/H-ui.js"></script>
<script type="text/javascript" src="/static/static/h-ui.admin/js/H-ui.admin.page.js"></script>
<!--/_footer /作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript">
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>
上述代码中加粗部分即是需要数据渲染的侧边栏。
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
// 使用基于合成器的类...
View::composer('base','App\Http\View\Composers\BaseComposer');
// 使用基于合成器的闭包...
View::composer('dashboard', function ($view) {
//
});
}
}
找到config\App.php,打开文件,找到”‘providers’ => [ ];”,然后在数组中加入“App\Providers\ViewServiceProvider::class,”,完成注册;
<?php
namespace App\Http\View\Composers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;
use App\Repositories\UserRepository;
class BaseComposer extends Controller
{
/**
* 实现 UserRepository
*
* @var UserRepository
*/
protected $users;
/**
* 创建一个新的 profile 合成器.
*
* @param UserRepository $users
* @return void
*/
/*public function __construct(UserRepository $users)
{
// Dependencies automatically resolved by service container...
$this->users = $users;
}*/
/**
* 将数据绑定到视图
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$menus = DB::table('xpcms_admin_menu')
->where(['status'=>0,'ishidden'=>0])
->orderBy('mid','asc')->cates('mid');
$menus= $this->gettreeitems($menus,$pid=0);
$data['menus'] = $menus;
$data['admin'] = session('admin');
$data['group'] = DB::table('xpcms_admin_group')->where('gid',$data['admin']['group_id'])->item();
$view->with($data);
}
//获取树形菜单
public function gettreeitems($items,$pid = 0)
{
$tree = [];
foreach ($items as $item){
if ($item['pid'] == $pid){
$tree[$item['mid']] = $item;
$tree[$item['mid']]['children'] = $this->gettreeitems($items,$item['mid']);
}
}
return $tree;
}
}
这样就完成了视图合成器的扩展,但是在使用过程中会提示public function __construct(UserRepository $users)这个构造方法引入的参数错误,见于目前自己对于PHP、Laravel知识的掌握,尝试多次未能解决报错问题,没有办法,暂时先注释掉,这样,不报错,数据也能正常渲染,但是最终会不会有其它影响,目前还不得而知,想请各位老师和朋友们有时间来解决这个问题。