第一步
把公共侧边栏放在public目录下,和公共footer.html 同一种方式引入到html页面中
第二步
建立公共类,让使用侧边栏的类继承这个公共类,就可以把数据显示在侧边栏中
具体步骤
在application /index/controller 下建立 Commen.php 公共类
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Request;
class Commen extends \think\Controller
类中,创建一个构造函数,取数据库中数据,assign 到页面中
public function __construct()
{
parent::__construct();
// 取出所有
$nei_list=DB::name('cate')->where('pid',53)->select(); // 内容分类
$zuo_list=DB::name('cate')->where('pid',23)->select(); // 作者分类
// 分配到 aside_my 中,展示个人中心
// echo '<pre>';
$user = session::get('USER_INFO');
$uid = $user['uid'];
$my = DB::name('user')->where('id',$uid)->find();
// 展示单个
// $this->assign('list',$nei_list);
// 展示多个时
$this->assign(array(
'nei_list'=>$nei_list,
'zuo_list'=>$zuo_list,
'my_list'=>$my,
));
}
而在需要用到侧边栏的类,只需要 继承 commen 类就可以了
展示的时候,只需要
return $this->fetch(‘展示的页面’);