api增删改查接口

左手的ㄟ右手 2022-12-28 00:37 368阅读 0赞

一个真正的php小白就在今天开始给大家分享我在学习php过程中遇到的问题,首先声明我是直接学的fastadmin框架,并没有去了解原生php

php写增删改查接口(没有进行验证)如果你也和我一样是小白,那么鉴权和验证也要了解一下的,我是用ApiPost进行接口测试的。

  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Controller;
  6. class Cs extends Api
  7. {
  8. //如果$noNeedLogin为空表示所有接口都需要登录才能请求
  9. //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
  10. //如果接口已经设置无需登录,那也就无需鉴权了
  11. // 无需登录的接口,*表示全部
  12. protected $noNeedLogin = ['add','deit'];
  13. // 无需鉴权的接口,*表示全部
  14. protected $noNeedright = [];
  15. //添加
  16. public function add()
  17. {
  18. if (\request()->isPost()) {
  19. $post = \request()->post();
  20. $file = \request()->file('image');
  21. if ($file) {
  22. $path = ROOT_PATH . 'puclic' . DS . 'uploads';
  23. $info = $file->move($path);
  24. if (!$info) {
  25. //上传失败
  26. $this->error('图片上传失败');
  27. }
  28. }
  29. $data = [
  30. 'name' => $post['name'],
  31. 'image'=> DS . 'uploads\\'.$info->getSaveName(),
  32. ];
  33. $shop = Db::name('ceshiapi')->insert($data);
  34. if ($shop) {
  35. $this->success('添加成功');
  36. } else {
  37. $this->error('添加失败');
  38. }
  39. }
  40. else {
  41. $this->error('请求方式错误');
  42. }
  43. }
  44. }

在写的时候我就不知道这是干什么的

在这里插入图片描述
在这里插入图片描述

  1. //删除
  2. public function del()
  3. {
  4. if (\request()->isPost()) {
  5. $del = \request()->post('id');
  6. $db = Db::name('ceshiapi')->where('id', '=', $del)->delete();
  7. if ($db) {
  8. $this->success('已删除');
  9. } else {
  10. $this->error('删除失败');
  11. }
  12. } else {
  13. $this->error('请求方式错误');
  14. }
  15. }
  16. //修改
  17. public function update()
  18. {
  19. if (\request()->isPost()) {
  20. $edit = \request()->post('id');
  21. $post = \request()->post();
  22. $data = [
  23. 'name' => $post['name'],
  24. ];
  25. $db = Db::name('ceshiapi')->where('id', '=', $edit)->update($data);
  26. if ($db) {
  27. $this->success('已更新');
  28. } else {
  29. $this->error('更新失败');
  30. }
  31. } else {
  32. $this->error('请求方式错误');
  33. }
  34. }
  35. //查询
  36. public function find()
  37. {
  38. if (\request()->isPost()) {
  39. $del = \request()->post('id');
  40. $db = Db::name('ceshiapi')->where('id', '=', $del)->find();
  41. if ($db) {
  42. $this->success('已查询',$db);
  43. } else {
  44. $this->error('查询失败');
  45. }
  46. } else {
  47. $this->error('请求方式错误');
  48. }
  49. }

发表评论

表情:
评论列表 (有 0 条评论,368人围观)

还没有评论,来说两句吧...

相关阅读

    相关 api增删接口

    一个真正的php小白就在今天开始给大家分享我在学习php过程中遇到的问题,首先声明我是直接学的fastadmin框架,并没有去了解原生php php写增删改查接口(没有进