最近开了一下thinkphp5,发现restful API没有做详细的介绍。于是就想搭建一个。言归正传,首先要看一下tp5手册,之后再来看一下,比较好一些。
二、项目
2.我们在v1下创建news.php 控制器。代码如下:
getNewsList();// 查询数据
if ($data) {
$code = 200;
} else {
$code = 404;
}
$data = [
'code' => $code,
'data' => $data
];
return json($data);
}
//获取详情 http://tp5.gzy/v1/News/3
public function view($id)
{
//$id = input('id');
//echo "$id";
// $id = input('id');
$model = new VendorNews;
//$data = $model::getNews($id);
$data = $model->getNews($id);// 查询数据
if ($data) {
$code = 200;
} else {
$code = 404;
}
$data = [
'code' => $code,
'data' => $data
];
return json($data);
}
//创建 http://tp5.gzy/v1/News
public function create()
{
$model = new VendorNews;
//$data = $model::getNews($id);
$postData = Request::post();
//var_dump() $postData;
$data = $model->getCreate($postData);// 查询数据
if ($data) {
$code = 201;
} else {
$code = 404;
}
$data = [
'code' => $code,
//'data' => $data
];
return json($data);
}
//更新 http://tp5.gzy/v1/News/25
public function update($id)
{
if(!$id){
json(["code"=>5001,"message"=>"没有获取到id"]);
}
$model = new VendorNews;
//$data = $model::getNews($id);
$postData = Request::param();
//var_dump() $postData;
$data = $model->getUpdate($postData,$id);// 查询数据
if ($data) {
$code = 201;
} else {
$code = 404;
}
$data = [
'code' => $code,
//'data' => $data
];
return json($id);
}
//删除 http://tp5.gzy/v1/News/25
public function delete($id)
{
if(!$id){
json(["code"=>5001,"message"=>"没有获取到id"]);
}
$model = new VendorNews;
//$data = $model::getNews($id);
$postData = Request::delete();
//var_dump() $postData;
$data = $model->getDelete($postData,$id);// 查询数据
if ($data) {
$code = 204;
} else {
$code = 404;
}
$data = [
'code' => $code,
//'data' => $data
];
return json($data);
}
}
3.我们在model下创建VendorNews.php
where('news_id', $id)->select();
//echo $this->getLastSql();
//$res='======';
return $res;
}
public function getNewsList()
{
$res = Db::name('vendor_news')->select();
// echo url('picture');
return $res;
}
public function getCreate($postData)
{
//var_dump(json_decode($postData));exit;
$data = [
"news_title" => $postData["news_title"],
"news_author" => $postData["news_author"],
"news_time" => time(),
];
$res = Db::name('vendor_news')->insert($data);
//echo $this->getLastSql();
// echo url('picture');
return $res;
}
public function getUpdate($postData,$id){
$data = [
"news_title" => $postData["news_title"],
"news_author" => $postData["news_author"],
];
$res = Db::name('vendor_news')->where('news_id',$id )->update($data);;
//echo $this->getLastSql();
// echo url('picture');
return $res;
}
public function getDelete($postData=[],$id){
$res = Db::name('vendor_news')->where('news_id',$id )->delete(["news_id"=>$id]);
//echo $this->getLastSql();
// echo url('picture');
return $res;
}
}
Route::get('think', function () {
return 'hello,ThinkPHP5!';
});
Route::get('hello/:name', function ($name) {
return $name ;
});
Route::get('hello/:name', 'index/hello');
//获取某一个活动
Route::get(':version/News/:id', 'api/:version.News/view')
->header('Access-Control-Allow-Origin','*')
->header('Access-Control-Allow-Credentials', 'true')
->allowCrossDomain(true);
//获取列表
Route::get(':version/News', 'api/:version.News/index');
//创建数据
Route::post(':version/News', 'api/:version.News/create');
//更新数据
Route::put(':version/News/:id', 'api/:version.News/update');//全部更新
Route::PATCH(':version/News/:id', 'api/:version.News/update');//更新个别字段
//删除
Route::delete(':version/News/:id', 'api/:version.News/delete');
//Route::options(':version/News', 'api/:version.News');
这样的话更新、创建、删除、列表、详情 基本就可以实现了如果涉及到跨域。
如果还不明白,可以参考一下资料。
1.tp5 手册
https://www.kancloud.cn/manual/thinkphp5_1/353960
2.put和PATCH区别
https://blog.csdn.net/qq_35264464/article/details/79490618
3http简介
http://www.cnblogs.com/ranyonsue/p/5984001.html