1.什么是无限极分类?
做法就好似以前laravel时候做的二级分类,表结构用parent_id对应所属分类
1.表结构,count字段可以不用
2.在控制器common.php当中。这里用了一个tree的函数,让parent_id与id对应起来,就是一种自关联,然后我们去定义tree这个助手函数
function __construct() //构造方法直接运行
{
parent::__construct(); //继承·
$this->check_login(); //直接在每一个页面去运行这个方法
$this->assign([
'menus' => $this->commonData()
]);
}
/***
*检测登录的
*
*/
public function check_login()
{
if (!SESSION('admin')) {
$this->error('您还没有登录,请登录','/admin/user/login');
}
}
/***
* 侧边栏--菜单
* @return mixed
*/
private function commonData()
{
$menu = ModelMenu::order('parent_id asc, sort_order asc, id asc')->select();
$menus = tree($menu);
return $menus;
}
3.自定义助手函数common.php定义函数,这里的tree其实就相当于我们之前所做的自关联,模板文件当中就是两层foreach
/**
* 无限极分类
* @param $data
* @param int $parent_id
* @param int $count
* @return mixed
*/
function tree(&$data, $parent_id = 0, $count = 0)
{
static $treeList; //定义静态变量
foreach ($data as $key => $value) {
if ($value['parent_id'] == $parent_id) {
$value['count'] = $count;
$treeList [] = $value;
unset($data[$key]); //unset() 销毁指定的变量。
tree($data, $value['id'], $count + 1);
}
}
return $treeList;
}
/***
* 子菜单名称定义空格缩进
* @param $count
* @return string
*/
function category_indent($count)
{
$str = '';
for ($i = 0; $i < $count; $i++) {
$str .= ' ';
}
return $str;
}
4.控制器Menu.php
<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\common\model\Menu as ModelMenu;
class Menu extends Common
{
public function __construct()
{
parent::__construct();
$this->assign([
'menu_active' => 'active',
'first_menus' => ModelMenu::where('parent_id', 0)->select()
]); //直接在构造里面查出顶级分类
}
/***
* 菜单首页
* @return mixed
*/
public function index()
{
return $this->fetch(); //因为直接继承了common.php所以直接加载首页就行了
}
/***
* 新增侧边栏菜单
* @return \think\response\View
*/
public function create()
{
return view('menu/create');
}
/***
* 执行新增
* @param Request $request
*/
public function store(Request $request)
{
$validate = new \app\admin\validate\Menu; //这是个验证器
if (!$validate->check($request->param())) { //如果未通过验证
$this->error($validate->getError());
} else {
$data = [
'parent_id' => $request->parent_id,
'title' => $request->title,
'module' => $request->module,
'controller' => $request->controller,
'action' => $request->action,
'is_forbidden' => $request->is_forbidden == 'on' ? 1 : 0,
'icon' => $request->icon,
'sort_order' => $request->sort_order,
];
ModelMenu::create($data);
$this->success('新增菜单成功', 'index');
}
}
/***
* 编辑
* @param $id
* @return \think\response\View
*/
public function edit($id)
{
$caidan = ModelMenu::find($id);
return view('menu/edit', compact('caidan'));
}
/***
* 执行编辑
* @param Request $request
* @param $id
*/
public function update(Request $request, $id)
{
$validate = new \app\admin\validate\Menu;
if (!$validate->check($request->param())) {
$this->error($validate->getError());
} else {
$data = [
'parent_id' => $request->parent_id,
'title' => $request->title,
'module' => $request->module,
'controller' => $request->controller,
'action' => $request->action,
'is_forbidden' => $request->is_forbidden == 'on' ? 1 : 0,
'icon' => $request->icon,
'sort_order' => $request->sort_order,
];
ModelMenu::where('id', $id)->update($data);
$this->success('编辑菜单成功', 'index');
}
}
/***
* 删除
* @param $id
*/
public function delete($id)
{
$menu = ModelMenu::where('parent_id', $id)->count();
if ($menu > 0) {
$data = ['status' => 0, 'msg' => '此菜单有二级菜单,删除请慎重!'];
} else {
ModelMenu::destroy($id);
$data = ['status' => 1, 'msg' => '删除菜单成功'];
}
return json($data); //这里是一个ajax删除,返回一个data数组过去,让前端提示消息
}
/***
* 启用、禁用和change_attr一个原理
* @param Request $request
*/
public function change_status(Request $request)
{
ModelMenu::where('id', $request->id)->setField('is_forbidden', !$request->attr);
return json(success_info('', '改变状态成功~'));
}
/***
* 排序
* @param Request $request
*/
public function sort_order(Request $request)
{
ModelMenu::where('id', $request->id)->setField('sort_order', $request->sort_order);
return json(success_info('', '您已成功排序~'));
}
}