thinkphp5实现无限级新闻分类
时间: 2023-11-22 20:55:14 浏览: 254
要实现无限级新闻分类,可以使用thinkphp5自带的树形结构扩展类库来实现。
1. 创建一个分类模型
在app\admin\model目录下创建一个NewsCategory.php模型文件。代码如下:
```php
namespace app\admin\model;
use think\Model;
use think\Db;
use think\Tree;
class NewsCategory extends Model
{
protected $name = 'news_category';
public function getTree()
{
$list = Db::name($this->name)->order('sort_order')->select();
$tree = new Tree();
$tree->init($list);
$tree = $tree->getTreeList($tree->getTreeArray(0), 'name');
return $tree;
}
}
```
2. 创建分类控制器
在app\admin\controller目录下创建一个NewsCategory.php控制器文件。代码如下:
```php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\NewsCategory as CategoryModel;
class NewsCategory extends Controller
{
public function index()
{
$category_model = new CategoryModel();
$tree = $category_model->getTree();
$this->assign('tree', $tree);
return $view->fetch();
}
public function add()
{
$category_model = new CategoryModel();
$tree = $category_model->getTree();
$this->assign('tree', $tree);
return $view->fetch();
}
public function save()
{
$data = input('post.');
$category_model = new CategoryModel();
$result = $category_model->validate('NewsCategory')->save($data);
if ($result === false) {
$this->error($category_model->getError());
} else {
$this->success('添加成功', url('index'));
}
}
public function edit($id)
{
$category_model = new CategoryModel();
$category = $category_model->get($id);
$tree = $category_model->getTree();
$this->assign('tree', $tree);
$this->assign('category', $category);
return $view->fetch();
}
public function update($id)
{
$data = input('post.');
$category_model = new CategoryModel();
$result = $category_model->validate('NewsCategory')->save($data, ['id' => $id]);
if ($result === false) {
$this->error($category_model->getError());
} else {
$this->success('编辑成功', url('index'));
}
}
public function delete($id)
{
$category_model = new CategoryModel();
$category = $category_model->get($id);
if (empty($category)) {
$this->error('分类不存在');
}
$result = $category->delete();
if ($result === false) {
$this->error('删除失败');
} else {
$this->success('删除成功');
}
}
}
```
3. 创建分类列表视图
在app\admin\view\news_category目录下创建一个index.html视图文件。代码如下:
```html
{extend name="layout/base"}
{block name="content"}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">分类列表</h3>
</div>
<div class="panel-body">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>分类名称</th>
<th>排序</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{volist name="tree" id="vo"}
<tr>
<td>{$vo.id}</td>
<td>{$vo.name}</td>
<td>{$vo.sort_order}</td>
<td>
<a href="{:url('edit', ['id' => $vo.id])}" class="btn btn-xs btn-primary"><i class="fa fa-edit"></i> 编辑</a>
<a href="{:url('delete', ['id' => $vo.id])}" class="btn btn-xs btn-danger confirm"><i class="fa fa-trash"></i> 删除</a>
</td>
</tr>
{/volist}
</tbody>
</table>
</div>
</div>
</div>
</div>
{/block}
```
4. 创建分类添加/编辑视图
在app\admin\view\news_category目录下创建一个add.html和edit.html视图文件。代码如下:
add.html
```html
{extend name="layout/base"}
{block name="content"}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">添加分类</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="{:url('save')}">
<div class="form-group">
<label for="name">分类名称</label>
<input type="text" class="form-control" name="name" id="name" placeholder="请输入分类名称" required>
</div>
<div class="form-group">
<label for="parent_id">上级分类</label>
<select class="form-control" name="parent_id" id="parent_id">
<option value="0">顶级分类</option>
{volist name="tree" id="vo"}
<option value="{$vo.id}">{$vo.title}</option>
{/volist}
</select>
</div>
<div class="form-group">
<label for="sort_order">排序</label>
<input type="text" class="form-control" name="sort_order" id="sort_order" placeholder="请输入排序" required>
</div>
<button type="submit" class="btn btn-primary">添加</button>
<button type="reset" class="btn btn-default">重置</button>
</form>
</div>
</div>
</div>
</div>
{/block}
```
edit.html
```html
{extend name="layout/base"}
{block name="content"}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">编辑分类</h3>
</div>
<div class="panel-body">
<form role="form" method="post" action="{:url('update', ['id' => $category->id])}">
<div class="form-group">
<label for="name">分类名称</label>
<input type="text" class="form-control" name="name" id="name" placeholder="请输入分类名称" value="{$category->name}" required>
</div>
<div class="form-group">
<label for="parent_id">上级分类</label>
<select class="form-control" name="parent_id" id="parent_id">
<option value="0">顶级分类</option>
{volist name="tree" id="vo"}
<option value="{$vo.id}" {if condition="$category->parent_id == $vo.id"}selected{/if}>{$vo.title}</option>
{/volist}
</select>
</div>
<div class="form-group">
<label for="sort_order">排序</label>
<input type="text" class="form-control" name="sort_order" id="sort_order" placeholder="请输入排序" value="{$category->sort_order}" required>
</div>
<button type="submit" class="btn btn-primary">保存</button>
<button type="reset" class="btn btn-default">重置</button>
</form>
</div>
</div>
</div>
</div>
{/block}
```
5. 创建分类验证器
在app\admin\validate目录下创建一个NewsCategory.php验证器文件。代码如下:
```php
namespace app\admin\validate;
use think\Validate;
class NewsCategory extends Validate
{
protected $rule = [
'name' => 'require|max:50',
'parent_id' => 'integer',
'sort_order' => 'integer',
];
protected $message = [
'name.require' => '分类名称不能为空',
'name.max' => '分类名称不能超过50个字符',
'parent_id.integer' => '上级分类ID必须是整数',
'sort_order.integer' => '排序必须是整数',
];
protected $scene = [
'save' => ['name', 'parent_id', 'sort_order'],
'update' => ['name', 'parent_id', 'sort_order'],
];
}
```
6. 创建分类数据表
在数据库中创建一个news_category表,包含id、name、parent_id、sort_order四个字段。其中,parent_id为0表示顶级分类,非0表示上级分类的id。
至此,无限级新闻分类的实现就完成了。
阅读全文