《使用ThinkPHP6开发项目》 - ThinkPHP6创建菜单模块

本文详细介绍了如何在CSDN平台上创建系统菜单表、设计数据库结构,以及使用ThinkPHP框架创建菜单控制器、模型和服务,处理菜单的增删改查和业务逻辑操作。
摘要由CSDN通过智能技术生成

#CSDN 年度征文|回顾 2023,赢专属铭牌等定制奖品#

一、创建菜单模块

1、创建系统菜单表

CREATE TABLE `menu` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '菜单ID',
  `menu_name` varchar(32) NOT NULL DEFAULT '' COMMENT '菜单名称',
  `path` varchar(255) NOT NULL DEFAULT '' COMMENT '路径',
  `redirect` varchar(255) NOT NULL COMMENT '跳转地址',
  `components` varchar(255) NOT NULL DEFAULT '' COMMENT '组件',
  `hidden` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '隐藏',
  `create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
  `pid` int(11) unsigned NOT NULL DEFAULT '1' COMMENT '父级菜单ID',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态:1.有效 0.无效',
  `is_del` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否删除',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `role_name` (`menu_name`) USING BTREE,
  KEY `status` (`status`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统菜单表';

2、使用命令创建菜单控制器

php think make:controller admin@Menu --plain

2、在Menu.php控制中创建菜单模块的方法

<?php
declare (strict_types = 1);

namespace app\admin\controller;

class Menu
{
    // 菜单列表
    public function list()
    {
        echo '菜单列表';
    }

    // 新增菜单
    public function create()
    {
        echo '新增菜单';
    }
    
    // 编辑菜单
    public function edit()
    {
        echo '编辑菜单';
    }

    // 删除菜单
    public function del()
    {
        echo '删除菜单';
    }

    // 启用/禁用菜单
    public function status()
    {
        echo '启用/禁用菜单';
    }
}

3、使用命令创建菜单模型

php think make:model admin@Menu

4、使用模型获取菜单数据

// 数据分页
public function data_page(array $where = [], array $pageData = [], array $sort = [], string $fields = '*'){
	if(empty($where)) $where = querymap();
	$result = $this->where($where)->field($fields)->order($sort)->paginate($pageData);
	if(is_object($result)) $result = $result->toArray();
	return $result;
}

// 数据列表
public function data_list(array $where = [], array $sort = [], string $fields = '*', $limit = 0){
	empty($where) and $where = querymap();
	if(empty($limit)){
		$result = $this->where($where)->field($fields)->order($sort)->select();
	}else{
		$result = $this->where($where)->field($fields)->limit($limit)->order($sort)->select();
	}
	if(is_object($result)) $result = $result->toArray();
	return $result;
}

// 数据值总和
public function data_sum(array $where = [], string $field, array $sort = []){
	$sum = $this->where($where)->order($sort)->sum($field);
	$result['sum'] = $sum;
	return $result;
}

// 数据量
public function data_count(array $where = [], array $sort = []){
	$count = $this->where($where)->order($sort)->count();
	$result['count'] = $count;
	return $result;
}

//新增数据
public function data_create(array $data)
{
	if(!empty($data[0]) && is_array($data[0])){
		$result = $this->saveAll($data);
		if(is_object($result)) $result = $result->toArray();
	}else{
		$data['create_time'] = time();
		$this->save($data);
		$data['id'] = $this->id;
		$result = $data;
	}
	return $result;
}

//更新数据
public function data_update(array $data = [], array $where = [])
{
	if(!empty($data[0]) && is_array($data[0])){
		$ids = array_column($data, 'id');
		$idlength = count($ids); $datalength = count($data);
		if($idlength != $datalength) return arrayData(500, '缺少更新条件');
		$result = $this->saveAll($data);
	}else{
		$result = $this->update($data, $where);
	}
	if(is_object($result)) $result = $result->toArray();
	if(empty($result)) $result = [];
	return $result;
}

//查看数据
public function data_view(array $where, string $field = "*", array $sort = [], array $hidden = [])
{
	$result = $this->where($where)->field($field)->order($sort)->hidden($hidden)->find();
	if(is_object($result)) $result = $result->toArray();
	if(empty($result)) {
		$result = [];
	}
	return $result;
}

//查看字段
public function data_value(array $where, string $field)
{
	$result = $this->where($where)->value($field);
	// if(is_object($result)) $result = $result->toArray();
	if(empty($result)) {
		$result = '';
	}
	return $result;
}

// 删除数据
public function data_delete(array $where, array $sort = [])
{
	$result = $this->where($where)->order($sort)->delete();
}

// 分组数据
public function data_group(array $where, string $group = '', string $field = '*', array $sort = [])
{
	$result = $this->where($where)->field($field)->group($group)->order($sort)->select();
	if(is_object($result)) $result = $result->toArray();
	if(is_null($result)) $result = [];
	return $result;
}

5、使用命令创建Service文件

php think make:service admin@Menu

6、创建菜单方法处理菜单业务逻辑

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃瓜的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值