前面我们简单讲了创建数据表
和应用目录结构
,这节开始正式开发后台模块。
首先,我们肯定需要对产品类容进行管理,那么就要在后台加上管理入口。
所以,我们先来创建个后台一级菜单。
创建一级菜单控制器
AdminIndexController.php
文件地址:.\app\product\controller\AdminIndexController.php
< php // +---------------------------------------------------------------------- // | Wien Designs [ WE CAN DO IT MORE SIMPLE ] // +---------------------------------------------------------------------- // | Copyright (c) 2018 http://www.wien.ren All rights reserved. // +---------------------------------------------------------------------- // | Author: Oliver Wien <oliverwien@yeah.net> // +---------------------------------------------------------------------- namespace app\product\controller; use cmf\controller\AdminBaseController; /** * Class AdminIndexController * @package app\product\controller * @adminMenuRoot( * 'name' =>'产品管理', * 'action' =>'default', * 'parent' =>'', * 'display'=> true, * 'order' => 10000, * 'icon' =>'cubes', * 'remark' =>'产品管理' * ) */ class AdminIndexController extends AdminBaseController { }
注:这里我们使用了后台菜单注释
,详细使用方式:https://www.kancloud.cn/thinkcmf/doc/299310
创建分类控制器
AdminCategoryController.php
文件地址:.\app\product\controller\AdminCategoryController.php
< php // +---------------------------------------------------------------------- // | Wien Designs [ WE CAN DO IT MORE SIMPLE ] // +---------------------------------------------------------------------- // | Copyright (c) 2018 http://www.wien.ren All rights reserved. // +---------------------------------------------------------------------- // | Author: Oliver Wien <oliverwien@yeah.net> // +---------------------------------------------------------------------- namespace app\product\controller; //继承CMF5的AdminBase控制器用于一些后台基础控制 use cmf\controller\AdminBaseController; use app\admin\model\RouteModel; use app\product\model\ProductCategoryModel; use think\Db; use app\admin\model\ThemeModel; class AdminCategoryController extends AdminBaseController { /** * 分类管理 * @adminMenu( * 'name' => '分类管理', * 'parent' => 'product/AdminIndex/default', * 'display'=> true, * 'hasView'=> true, * 'order' => 20, * 'icon' => '', * 'remark' => '分类管理', * 'param' => '' * ) */ public function index() { //实例化项目分类模型 $productCategoryModel = new ProductCategoryModel(); //将分类数据生成树结构 $categoryTree = $productCategoryModel->adminCategoryTableTree(); $this->assign('category_tree', $categoryTree); return $this->fetch(); } }
修改ProductCategoryModel.php
文件地址:.\app\product\model\ProductCategoryModel.php
< php // +---------------------------------------------------------------------- // | Wien Designs [ WE CAN DO IT MORE SIMPLE ] // +---------------------------------------------------------------------- // | Copyright (c) 2018 http://www.wien.ren All rights reserved. // +---------------------------------------------------------------------- // | Author: Oliver Wien <oliverwien@yeah.net> // +---------------------------------------------------------------------- namespace app\product\model; use think\Model; use tree\Tree; class ProductCategoryModel extends Model { /** * 生成分类的table格式的树形结构 * @param int|array $currentIds * @param string $tpl * @return string */ public function adminCategoryTableTree($currentIds = 0, $tpl = '') { //生成查询条件,删除时间默认为0,即未删除状态 $where = ['delete_time' => 0]; //将数据查询并存入数组中 $categories = $this->order("list_order ASC")->where($where)->select()->toArray(); //CMF5的Tree库,位于simplewind\extend\tree\Tree.php $tree = new Tree(); $tree->icon = [' │', ' ├─', ' └─']; $tree->nbsp = ' '; //判断变量类型是否为数组类型,不是的话将其转换成数组形式 if (!is_array($currentIds)) { $currentIds = [$currentIds]; } //创建一个新的数组,将查询出来的数据遍历后加上链接等想要的参数 $newCategories = []; foreach ($categories as $item) { //检测是否为当前选中栏目 $item['checked'] = in_array($item['id'], $currentIds) "checked" : ""; //加上当前栏目的URL链接 $item['url'] = cmf_url('project/List/index', ['id' => $item['id']]); //加上添加,修改文字及链接 $item['str_action'] = '<a href="' . url("AdminCategory/add", ["parent" => $item['id']]) . '">添加子分类</a> | <a href="' . url("AdminCategory/edit", ["id" => $item['id']]) . '">' . lang('EDIT') . '</a> | <a class="js-ajax-delete" href="' . url("AdminCategory/delete", ["id" => $item['id']]) . '">' . lang('DELETE') . '</a> '; //创建新的数组 array_push($newCategories, $item); } //将新的数组进行初始化 $tree->init($newCategories); //定义数的展现结构,这边采用的是表格的<tr><td> if (empty($tpl)) { $tpl = "<tr> <td><input name='list_orders[\$id]' type='text' size='3' value='\$list_order' class='input-order'></td> <td>\$id</td> <td>\$spacer <a href='\$url' target='_blank'>\$name</a></td> <td>\$description</td> <td>\$str_action</td> </tr>"; } $treeStr = $tree->getTree(0, $tpl); return $treeStr; } }
创建后台模板文件
文件位置:.\themes\admin_simpleboot3\project\admin_category\index.html
<!-- 调用后台头部文件 --> <include file="public@header"/> </head> <body> <h2>This is product app template!</h2> <p>这是产品应用模板:.\themes\admin_simpleboot3\project\admin_category\index.html</p> </body> </html>
导入新菜单
按F5刷新页面
今天就先讲到这里吧,有什么问题,可以在下方评论或直接联系我。