Organize and summarize how thinkphp5 uses restful
Create api directory
Create a controller and configuration file in the api directory
As shown below:
api test controller
<?php
namespace app\api\controller;
use think\Controller;
class Test extends Controller
{
public function index()
{
return [
'status' => 0,
'msg' => 'success'
];
}
//put 需要定义$id
public function update($id = '')
{
$put_data = input('put.');
$data = array(
'id' => $id,
'put_data' => $put_data
);
return $data;
}
//delete 需要定义$id 然后用提交的id值替换
public function delete($id = '')
{
$data = array(
'id' => $id,
);
return $data;
}
//post 需要定义$id
public function save()
{
$post_data = input('post.');
$data = array(
'post_data' => $post_data
);
return $data;
}
}
Configure the return format in config.php
<?php
return [
//设置返回数据类型为json格式
'default_return_type'=>'json',
];
Set up routing
There is a route.php file in the thinkphpwu/application directory
Automatically register routing rules
Use __rest__ to add resource routing definitions in the routing configuration file:
return [
// 定义资源路由
'__rest__'=>[
// 指向index模块的blog控制器
'blog'=>'index/blog',
],
// 定义普通路由
'hello/:id'=>'index/hello',
]
7 routing rules will be automatically registered after setting
Logo | Request type | Generate routing rules | Corresponding operation method (default) |
---|---|---|---|
index | GET | blog | index |
create | GET | blog/create | create |
save | POST | blog | save |
read | GET | blog/:id | read |
edit | GET | blog/:id/edit | edit |
update | PUT | blog/:id | update |
delete | DELETE | blog/:id | delete |
The specific controller pointed to is determined by the routing address. For example, the above setting will correspond to the blog controller of the index module. You only need to create the above corresponding operation method for the Blog controller to support the following URL access:
http://serverName/blog/
http://serverName/blog/128
http://serverName/blog/28/edit
The corresponding method in the Blog controller is as follows:
namespace app\index\controller;
class Blog {
public function index(){
}
public function read($id){
}
public function edit($id){
}
}
Set routing separately
The specific code is as follows:
<?php
//引用think的路由底层Route类
use think\Route;
//设置路由
//get方式
Route::get('test', 'api/test/index');
//put方式
Route::put('test/:id', 'api/test/update');
//delete方式
Route::delete('test/:id', 'api/test/delete');
//post方式
//Route::post('test', 'api/test/save');
//resource方式 对应7种方式
Route::resource('test', 'api/test');
Route::resource('test','api/test'); This method includes post request, automatically find the corresponding save method
get request
Return to the content of the background settings
put request
The request path is http://app.thinkphpwu.com/test/888
corresponding to 888 which means id, put_data is the data we submitted, and the background can be modified according to our own business.
delete request
The request is the delete method, which returns the value of the id to be deleted
post request
Generalized API interface data encapsulation
Add the show method to the common function common.php file in the application directory
/**
* 通用化API接口数据输出
* @param int $status 业务状态码
* @param string $message 信息提示
* @param [] $data 数据
* @param int $httpCode http状态码
* @return array
*/
function show($status, $message, $data = [], $httpCode = 200)
{
$data = [
'status' => $status,
'message' => $message,
'data' => $data,
];
return json($data, $httpCode);
}
Call the show method in the function that needs to be returned to the front end. For example, we call show in the save method of the test controller. The code is as follows:
public function save()
{
$post_data = input('post.');
$data = array(
'post_data' => $post_data
);
return show(0, '保存成功!', $data);
}
Use the postman request to get the returned data as shown below: