# RESTful接口設計
## REST控制器核心基類
繼承 Rest 控制器即可 使用
* 自動把GET,POST,PUT,DELETE 映射到 對應的Action 如getdetail 映射到GET\_detailAction()
* 自動綁定參數id
* 自動輸出json格式數據
代碼演示:
~~~php
class IndexController extends Rest
{
/**
* GET /Index/index?data=''
* GET請求測試
*
* success() 和 fail() 快速返回示例
*/
public function GET_indexAction()
{
//這是一個用戶查詢例子
$uid = input('uid');
//簡單的模型調用
$user_model = new UserModel();
$userInfo = $user_model->getUserInfo($uid);
}
/**
* POST /Index/addUser
* POST請求測試
*
* response()函數自定義狀態
*/
public function POST_addUserAction()
{
$where['username'] = input('username');
$where['password'] = input('password');
//數據驗證
$video_validate = new \validate\User();
//采用場景驗證
if (!$video_validate->scene('add')->check($where)) {
$this->fail($video_validate->getError());
}
//簡單的模型調用
$user_model = new UserModel();
if ($user_model->add($where)) {
$this->success();
}
$this->fail();
}
}
~~~
$this->success() 快速返回成功
$this->fail() 快速返回錯誤
$this->response(); 設置響應狀態
可以參考github代碼里的案例
- 序言
- 安裝 Yaf
- Yaf基礎知識
- 1.運行流程
- 2.YAF架構
- 3.目錄結構
- 4.Yaf的配置
- 5.Yaf的Bootstrap
- 6.Yaf的多模塊配置
- 7.Yaf中使用命名空間
- 本書框架配置
- 1.框架目錄結構
- 2.數據庫配置
- 3.緩存配置
- 4.全局配置
- 5.公共助手函數
- 請求與響應
- 1.請求-Request
- 2.響應-Response
- 數據庫操作
- 使用think-orm
- 接口開發
- 1.RESTful接口設計
- 2.Yar RPC接口設計
- 數據驗證
- 數據驗證 - validate
- 網頁開發
- Session
- Cookie
- 路由設置
- 工具類
- 1.Rsa加密
- 2.Random快速生成隨機數
- 3.Cache - 緩存
- 4.Weapp - 微信小程序類
- 5.Qiniu - 七牛云存儲使用
- 6.支付類(微信&支付寶)
- 7.Logs - 日志記錄