第一天
首先我们要知道Restful Api 的编码规范 从这里开始我们的编码规范都按照这个Restful来
生成标准API
- 我们创建一个 Base 抽象基类,专门用于提供给子类实现 API 生成;
abstract class Base
{
protected function create($data, $msg = '', $code = 200, $type = 'json')
{
$result = [
'code' => $code,
'msg' => $msg,
'data' => $data
];
return Response::create($result, $type);
}
}
- 通过 Base 基类的继承,我们想创建一个获取全部用户列表的 API 接口;
public function index()
{
$data = UserModel::field('id,username,gender,email')->select();
return $this->create($data, $data->isEmpty() ? '数据不存在' : '数据请求成功');
}
第二天
1、接口分页API
- 在基类 Base 基类中编写分页的方法
protected $page;
protected $pageSize;
public function __construct()
{
$this->page = (int)Request::param('page');
$this->pageSize = (int)Request::param('page_size', 5);
}
上面这个方法可以,但是有的可能会报错。(就比如我报了静态调用的错)那我们可以使用助手函数,这个也比较方便
protected $page;
protected $pageSize;
public function __construct()
{
$this->page = $this->request->param('page');
$this->pageSize = $this->request->param('page_size',5);
}
- 使用这个分类
$data = UserMode::field('id,username,prevtime,logintime,jointime')
->page($this->page,$this->pageSize)
->select();
2、单数据API
- 请求单数据的api没什么好说的。
public function read($id)
{
if (!is_numeric($id)) {
return $this->error('id 参数错误, ~', []);
}
$data = UserMode::field('id,username,prevtime,logintime,jointime,email')->find($id);
if (empty($data)){
return $this->success('请求成功无数据',[]);
} else {
return $this->success('数据请求成功',$data);
}
}
3、新增数据API
- 首先是这样的,我们要将这个数据进行严重,然后再将他添加进数据库里,然后返回出他的状态以及id
public function save(Request $request)
{
if ($this->request->isPost()) {
$username = $this->request->post('username');
$password = $this->request->post('password');
$email = $this->request->post('email');
$mobile = $this->request->post('mobile');
$data = [
'username' => $username,
'password' => $password,
'email' => $email,
'nickname' => $username,
'mobile' => $mobile
];
$validate = validate('User');
if(!$validate->check($data)){
return $this->error($validate->getError(), []);
} else {
$data['password'] = sha1($data['password']);
$id = UserMode::create($data)->getData('id');
if (empty($id)){
return $this->success('添加失败',[]);
} else {
return $this->success('数据请求成功',$id);
}
}
} else {
echo '不是post请求';
}
}
第三天
1、删除数据API
- 首先判断,获取数据后删除
public function delete($id)
{
if (!is_numeric($id)) {
return $this->error('id 参数错误, ~', [],400);
}
try {
UserMode::find($id)->delete();
return $this->success('删除成功',[],200);
} catch (\Error $exception) {
return $this->error('数据不存在或无法删除',[],400);
}
}
2、修改数据API
- 简单的修改数据API,这边我修改的email,修改密码什么的也都差不多只不过要解密加密而已
public function update(Request $request,$id)
{
$data = $request->param();
$validate = validate('User');
if(!$validate->scene('edit')->check($data)){
return $this->error($validate->getError(), []);
}
$updateDate = UserMode::find($id);
if ($updateDate->email === $data['email']) {
return $this->error('您并没有修改', [],400);
}
$id = UserMode::update($data)->getData('id');
if (empty($id)){
return $this->success('修改失败',$id);
} else {
return $this->success('修改成功',$id);
}
}
3、关联数据API
- 就例如,某个人所有的评论啊、文章啊,之类的 第一步先关联
public function comments()
{
return $this->hasMany('CmsComment','user_id','id');
}
- 第二步简简单单
public function comment($id)
{
if (!is_numeric($id)) {
return $this->error('id 参数错误, ~', [],400);
}
$data = UserMode::find($id)->comments()->field('id,content')->select();
if (empty($data)){
return $this->success('无数据',$id);
} else {
return $this->success('数据请求成功',$data);
}
}
|