Three, RestFul combat
Resource route 1, TP Framework
Manual - Routing - routing resources, manuals - Controller - Resource Controller
① create api module
php think build --module api
② create news Controller
php think make:controller api/News
③ setting route (application / route.php)
\think\Route::resource('news','api/news');
Equivalent respectively set up the following routes:
\think\Route::get('news','api/news/index');
\think\Route::get('news/create','api/news/create');
\think\Route::post('news','api/news/save');
\think\Route::get('news/:id','api/news/read');
\think\Route::get('news/:id/edit','api/news/edit');
\think\Route::put('news/:id','api/news/update');
\think\Route::delete('news/:id','api/news/delete');
7 is automatically registered after setting routing rules, as follows:
Mark | Request type | Generation routing rules | The corresponding method of operation (default) |
---|---|---|---|
index | GET | news | index |
create | GET | news/create | create |
save | POST | news | save |
read | GET | news/:id | read |
edit | GET | news/:id/edit | edit |
update | PUT | news/:id | update |
delete | DELETE | news/:id | delete |
④ modify News controller, the data format returns json
<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
class News extends Controller
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'index']);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'create']);
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'save']);
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'read']);
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'edit']);
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'update']);
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
return json(['code' => 200, 'msg' => 'success', 'data'=>'delete']);
}
}
Visit the following address by postman seven are:
请求方式 请求地址
get http://www.tpshop.com/news
get http://www.tpshop.com/news/create
post http://www.tpshop.com/news
get http://www.tpshop.com/news/33
get http://www.tpshop.com/news/33/edit
put http://www.tpshop.com/news/33
delete http://www.tpshop.com/news/33
2, ajax request restful Interface
Under public directory, create a test file api.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ajax请求restful接口</title>
<script src="/static/admin/js/jquery-1.8.1.min.js"></script>
</head>
<body>
<input type="button" id="index" value="index">
<input type="button" id="create" value="create">
<input type="button" id="save" value="save">
<input type="button" id="read" value="read">
<input type="button" id="edit" value="edit">
<input type="button" id="update" value="update">
<input type="button" id="delete" value="delete">
<script>
$(function(){
$('#index').click(function(){
$.ajax({
"url":"/news",
"type":"get",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#create').click(function(){
$.ajax({
"url":"/news/create",
"type":"get",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#save').click(function(){
$.ajax({
"url":"/news",
"type":"post",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#read').click(function(){
$.ajax({
"url":"/news/33",
"type":"get",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#edit').click(function(){
$.ajax({
"url":"/news/33/edit",
"type":"get",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#update').click(function(){
$.ajax({
"url":"/news/33",
"type":"put",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#delete').click(function(){
$.ajax({
"url":"/news/33",
"type":"delete",
"data":"",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
});
</script>
</body>
</html>
3, the request disguise
Some clients (such as a low version of the browser) may only support a get request, post requests, does not support the delete request and put the request.
TP Framework provides support for "Request camouflage" can be used to carry _method post request parameters, disguised as other requests.
Such as the use of post ajax request put request disguise
public / api.html add the following code
<input type="button" id="post_to_put" value="伪装put">
<input type="button" id="post_to_delete" value="伪装delete">
<script>
$(function(){
$('#post_to_put').click(function(){
$.ajax({
"url":"/news/33",
"type":"post",
"data":"_method=put",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
$('#post_to_delete').click(function(){
$.ajax({
"url":"/news/33",
"type":"get",
"data":"_method=delete",
"dataType":"json",
"success":function(res){
console.log(res);
}
});
});
})
</script>
4, Restful common routing resources
Add page page display method create and modify pages page shows generally can not edit method.
Mark | Request type | Generation routing rules | The corresponding method of operation (default) | Remark |
---|---|---|---|---|
index | GET | news | index | Query multiple data (list) |
read | GET | news/:id | read | A query data (details, page modify display) |
save | POST | news | save | A new data |
update | PUT | news/:id | update | Modify a data |
delete | DELETE | news/:id | delete | To delete a data |
5, the actual development of Restful
Restful interface is usually returned data model is complete, the particle size is too "rough", unfriendly client (client may need only a small part of the field).
Restful typical usage scenarios: an open API (data from various open platform api). The reason why open API open, is because they do not know or care what the client needs to return the results directly back complete data, the benefits are universal.
The actual development, often internal interface development, demand is very clear, so are generally flexible and learn the advantages of Restful, combined with their own situation, to design their own internal api, in addition to the basic CRUD interface, usually Some service interface design will (according to the service logic needed, a data interface to integrate a plurality of resources back).