MixPHP 是一款基于 Swoole 的常駐內存型 PHP 高性能框架,框架的高性能特點非常適合開發 API 接口,而且 MixPHP 非常接近傳統 MVC 框架,所以開發接口時非常簡單。
下面做一個開發 API 接口的簡單實例:
從 articles 表,通過 id 獲取一篇文章。
訪問該接口的 URL:
http://www.e.com/articles/details?id=1
數據庫表結構如下:
CREATE TABLE `articles` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `content` varchar(255) NOT NULL, `dateline` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;第一步
修改數據庫配置文件,MixPHP 的應用配置文件中,關于數據庫的信息都引用了 common/config/database.php 文件。
修改應用配置文件:
修改 Response 組件默認輸出格式為 JSON 格式。
修改 404/500 錯誤輸出格式為 JSON 格式。
框架默認的 404/500 響應是網頁,而 API 服務需要響應 JSON 數據,通常其他傳統 MVC 框架需要修改很多地方才可完成這個需求,MixPHP 本身就提供該種配置,只需修改一下配置即可。
MixPHP 的默認 Web 應用中有兩個配置文件,分別為:
main.php : 部署在 mix-httpd 時使用。
main_compatible.php :部署在 Apache/PHP-FPM 時使用。
開發 API 時我們推薦在 Apache/PHP-FPM 下開發,上線再部署至 mix-httpd 即可,反正是無縫切換的。
現在我們修改 response 鍵名下的 defaultFormat 鍵為 mix/http/Error::FORMAT_JSON,如下:
// 響應 response = [ // 類路徑 html' target='_blank'>class = mix/http/compatible/Response , // 默認輸出格式 defaultFormat = mix/http/Response::FORMAT_JSON, // json json = [ // 類路徑 class = mix/http/Json , // jsonp jsonp = [ // 類路徑 class = mix/http/Jsonp , // callback鍵名 name = callback , // xml xml = [ // 類路徑 class = mix/http/Xml ,],
然后修改 main_compatible.php 文件中 error 鍵名下的 format 鍵為 mix/http/Error::FORMAT_JSON,如下:
// 錯誤 error = [ // 類路徑 class = mix/http/Error , // 輸出格式 format = mix/http/Error::FORMAT_JSON,],第三步
創建控制器:
apps/index/controllers/ArticlesController.php
?phpnamespace apps/index/controllers;use mix/facades/Request;use mix/http/Controller;use apps/index/messages/ErrorCode;use apps/index/models/ArticlesForm;class ArticlesController extends Controller public function actionDetails() // 使用模型 $model = new ArticlesForm(); $model- attributes = Request::get(); $model- setScenario( actionDetails if (!$model- validate()) { return [ code = ErrorCode::INVALID_PARAM]; // 獲取數據 $data = $model- getDetails(); if (!$data) { return [ code = ErrorCode::ERROR_ID_UNFOUND]; // 響應 return [ code = ErrorCode::SUCCESS, data = $data];}
創建錯誤碼類:
apps/index/messages/ErrorCode.php
?phpnamespace apps/index/messages;class ErrorCode const SUCCESS = 0; const INVALID_PARAM = 100001; const ERROR_ID_UNFOUND = 200001;}
創建表單驗證模型:
apps/index/models/ArticlesForm.php
?phpnamespace apps/index/models;use mix/validators/Validator;use apps/common/models/ArticlesModel;class ArticlesForm extends Validator public $id; // 規則 public function rules() return [ id = [ integer , unsigned = true, maxLength = 10], // 場景 public function scenarios() return [ actionDetails = [ required = [ id ]], // 獲取詳情 public function getDetails() return (new ArticlesModel())- getRowById($this- }
創建數據表模型:
apps/common/models/ArticlesModel.php
?phpnamespace apps/common/models;use mix/facades/RDB;class ArticlesModel const TABLE = articles // 獲取一行數據通過id public function getRowById($id) $sql = SELECT * FROM ` . self::TABLE . ` WHERE id = :id $row = RDB::createCommand($sql)- bindParams([ id = $id, ])- queryOne(); return $row;}
以上就是全部代碼的編寫。
第四步使用 Postman 測試,如下:
接口開發與測試完成,是不是很簡單呀。
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP !
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答