-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
--- | ||
id: 3 | ||
title: 使用fastadmin创建查询、增加、修改、删除数据接口 | ||
date: 2024-3-21 | ||
category: 后端 | ||
description: 利用fastadmin框架的API基础,创建查询、增加、修改、删除数据接口 | ||
tags: ['Fastadmin','Api'] | ||
published: true | ||
--- | ||
|
||
## 模型 | ||
|
||
```php [application/api/model.php] | ||
<?php | ||
namespace app\api\model; | ||
use think\Model; | ||
class Test extends Model{ | ||
// 对应的数据表 | ||
protected $table = 'fa_cms_block'; | ||
// 允许写入的字段 | ||
protected $allowField = ['type', 'name', 'title','status','others']; | ||
} | ||
``` | ||
## 控制器 | ||
|
||
```php [application/api/controller.php] | ||
<?php | ||
// 声明命名空间,这个控制器属于 app\api\controller 这个命名空间 | ||
namespace app\api\controller; | ||
|
||
// 引入 app\common\controller\Api 类,这个类可能包含了一些公共的方法或属性 | ||
use app\common\controller\Api; | ||
|
||
// 引入 think\Request 类,用于处理 HTTP 请求 | ||
use think\Request; | ||
|
||
use app\api\controller\Validate as TestValidate; | ||
// 引入 app\api\model\Test 类,并重命名为 TestModel,用于数据库操作 | ||
use app\api\model\Test as TestModel; | ||
|
||
// 定义一个名为 Test 的类,它继承自 Api 类 | ||
class Test extends Api | ||
{ | ||
// 定义一个受保护的属性 $noNeedLogin,用于指定哪些方法不需要登录即可访问 | ||
protected $noNeedLogin = ['index','add','update','delete']; | ||
// 无需鉴权的接口,*表示全部 | ||
protected $noNeedRight = []; | ||
|
||
public function _initialize() { | ||
parent::_initialize(); | ||
} | ||
|
||
public function index(Request $request) { | ||
$validate = new TestValidate(); | ||
// 从请求中获取 'name' 参数的值 | ||
$name = $request->get('name'); | ||
// 定义一个空数组 $map,用于存储查询条件 | ||
$map =[]; | ||
// 检查 $name 是否为空 | ||
if (empty($name)) { | ||
$this->error(__('参数不正确')); | ||
} else { | ||
$map['name'] = $name; | ||
}; | ||
// 实例化 TestModel 类 | ||
$testModel = new TestModel(); | ||
|
||
// 使用 TestModel 的 where 方法根据 $map 数组中的条件进行查询 | ||
// 使用 field 方法指定查询的字段 | ||
// 使用 select 方法执行查询并返回结果 | ||
$data = $testModel->where($map) | ||
->field('type,name,title,image,url,content,parsetpl,weigh,createtime,updatetime,begintime,endtime,status') | ||
->select(); | ||
// 检查查询结果是否为空 | ||
if (empty($data)) { | ||
// 如果为空,则返回 'No data found' 的成功消息 | ||
$this->success('No data found', [], 200); | ||
} | ||
// 如果查询结果不为空,则返回 '返回成功' 的成功消息以及查询到的数据 | ||
$this->success('返回成功', $data,200); | ||
} | ||
|
||
/** | ||
* 增加数据 | ||
* | ||
* @param Request $request HTTP请求对象 | ||
*/ | ||
public function add(Request $request) | ||
{ | ||
// 获取请求参数 | ||
$data = $request->post(); | ||
$validate = new TestValidate(); | ||
// 实例化TestModel类 | ||
$testModel = new TestModel(); | ||
|
||
// 使用TestModel的save方法保存数据 | ||
$result = $testModel->save($data); | ||
|
||
if ($result) { | ||
// 保存成功,返回成功消息 | ||
$this->success('数据添加成功', $testModel->getData(), 201); | ||
} else { | ||
// 保存失败,返回失败消息 | ||
$this->error('数据添加失败'); | ||
} | ||
} | ||
|
||
/** | ||
* 修改数据 | ||
* | ||
* @param Request $request HTTP请求对象 | ||
*/ | ||
public function update(Request $request) | ||
{ | ||
// 实例化验证器 | ||
$validate = new TestValidate(); | ||
|
||
// 获取请求数据 | ||
$data = $request->put(); | ||
$id = $request->param('id'); | ||
|
||
// 验证数据 | ||
// if (!$validate->check($data)) { | ||
// // 验证失败,返回错误信息 | ||
// $this->error($validate->getError()); | ||
// } | ||
|
||
// 实例化模型 | ||
$model = TestModel::get($id); | ||
|
||
if (!$model) { | ||
// 模型不存在,返回失败消息 | ||
$this->error('数据不存在'); | ||
} | ||
|
||
// 尝试更新数据 | ||
if ($model->save($data)) { | ||
// 更新成功,返回成功消息 | ||
$this->success('数据更新成功'); | ||
} else { | ||
// 更新失败,返回失败消息 | ||
$this->error('数据更新失败'); | ||
} | ||
} | ||
|
||
/** | ||
* 删除数据 | ||
* | ||
* @param Request $request HTTP请求对象 | ||
*/ | ||
public function delete(Request $request) | ||
{ | ||
// 获取要删除的数据的id | ||
$id = $request->param('id'); | ||
|
||
// 验证id是否存在 | ||
if (empty($id)) { | ||
$this->error('缺少数据ID'); | ||
} | ||
|
||
// 实例化模型 | ||
$model = TestModel::get($id); | ||
|
||
if (!$model) { | ||
// 模型不存在,返回失败消息 | ||
$this->error('数据不存在'); | ||
} | ||
|
||
// 尝试删除数据 | ||
if ($model->delete()) { | ||
// 删除成功,返回成功消息 | ||
$this->success('数据删除成功'); | ||
} else { | ||
// 删除失败,返回失败消息 | ||
$this->error('数据删除失败'); | ||
} | ||
} | ||
} | ||
``` |