api增刪改查介面
阿新 • • 發佈:2020-12-13
技術標籤:php寫增刪改查api介面phpapi
一個真正的php小白就在今天開始給大家分享我在學習php過程中遇到的問題,首先宣告我是直接學的fastadmin框架,並沒有去了解原生php
php寫增刪改查介面(沒有進行驗證)如果你也和我一樣是小白,那麼鑑權和驗證也要了解一下的,我是用ApiPost進行介面測試的。
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Controller;
class Cs extends Api
{
//如果$noNeedLogin為空表示所有介面都需要登入才能請求
//如果$noNeedRight為空表示所有介面都需要驗證許可權才能請求
//如果介面已經設定無需登入,那也就無需鑑權了
// 無需登入的介面,*表示全部
protected $noNeedLogin = ['add','deit'];
// 無需鑑權的介面,*表示全部
protected $noNeedright = [];
//新增
public function add()
{
if (\request()->isPost()) {
$post = \request()->post( );
$file = \request()->file('image');
if ($file) {
$path = ROOT_PATH . 'puclic' . DS . 'uploads';
$info = $file->move($path);
if (!$info) {
//上傳失敗
$this->error('圖片上傳失敗');
}
}
$data = [
'name' => $post['name'],
'image'=> DS . 'uploads\\'.$info->getSaveName(),
];
$shop = Db::name('ceshiapi')->insert($data);
if ($shop) {
$this->success('新增成功');
} else {
$this->error('新增失敗');
}
}
else {
$this->error('請求方式錯誤');
}
}
}
在寫的時候我就不知道這是幹什麼的
//刪除
public function del()
{
if (\request()->isPost()) {
$del = \request()->post('id');
$db = Db::name('ceshiapi')->where('id', '=', $del)->delete();
if ($db) {
$this->success('已刪除');
} else {
$this->error('刪除失敗');
}
} else {
$this->error('請求方式錯誤');
}
}
//修改
public function update()
{
if (\request()->isPost()) {
$edit = \request()->post('id');
$post = \request()->post();
$data = [
'name' => $post['name'],
];
$db = Db::name('ceshiapi')->where('id', '=', $edit)->update($data);
if ($db) {
$this->success('已更新');
} else {
$this->error('更新失敗');
}
} else {
$this->error('請求方式錯誤');
}
}
//查詢
public function find()
{
if (\request()->isPost()) {
$del = \request()->post('id');
$db = Db::name('ceshiapi')->where('id', '=', $del)->find();
if ($db) {
$this->success('已查詢',$db);
} else {
$this->error('查詢失敗');
}
} else {
$this->error('請求方式錯誤');
}
}