注意:(在使用資源控制器時,盡量隱藏入口文件 )
在根目錄/pubilc/.htaccess 放入以下代碼:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>
① 創建api模塊
php think build --module api
② 創建news控制器
php think make:controller api/News
③ 設置路由(application/route.php)
\think\Route::resource('news','api/news');
相當於分別設置了以下路由:
\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個路由規則,如下:
標識 | 請求類型 | 生成路由規則 | 對應操作方法(默認) |
---|---|---|---|
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 |
④ 修改News控制器,返回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']); } }
通過postman 分別訪問以下七個地址:
請求方式 請求地址 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
public目錄下,創建測試文件 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>