MixPHP 是一款基于 Swoole 的常駐內(nèi)存型 PHP 高性能框架,框架的高性能特點(diǎn)非常適合開發(fā) API 接口,而且 MixPHP 非常接近傳統(tǒng) MVC 框架,所以開發(fā)接口時(shí)非常簡單。
下面做一個(gè)開發(fā) API 接口的簡單實(shí)例:
從 articles 表,通過 id 獲取一篇文章。
訪問該接口的 URL:
http://www.e.com/articles/details?id=1
數(shù)據(jù)庫表結(jié)構(gòu)如下:
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;第一步
修改數(shù)據(jù)庫配置文件,MixPHP 的應(yīng)用配置文件中,關(guān)于數(shù)據(jù)庫的信息都引用了 common/config/database.php 文件。
修改應(yīng)用配置文件:
修改 Response 組件默認(rèn)輸出格式為 JSON 格式。
修改 404/500 錯(cuò)誤輸出格式為 JSON 格式。
框架默認(rèn)的 404/500 響應(yīng)是網(wǎng)頁,而 API 服務(wù)需要響應(yīng) JSON 數(shù)據(jù),通常其他傳統(tǒng) MVC 框架需要修改很多地方才可完成這個(gè)需求,MixPHP 本身就提供該種配置,只需修改一下配置即可。
MixPHP 的默認(rèn) Web 應(yīng)用中有兩個(gè)配置文件,分別為:
main.php : 部署在 mix-httpd 時(shí)使用。
main_compatible.php :部署在 Apache/PHP-FPM 時(shí)使用。
開發(fā) API 時(shí)我們推薦在 Apache/PHP-FPM 下開發(fā),上線再部署至 mix-httpd 即可,反正是無縫切換的。
現(xiàn)在我們修改 response 鍵名下的 defaultFormat 鍵為 mix/http/Error::FORMAT_JSON,如下:
// 響應(yīng) response = [ // 類路徑 html' target='_blank'>class = mix/http/compatible/Response , // 默認(rèn)輸出格式 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,如下:
// 錯(cuò)誤 error = [ // 類路徑 class = mix/http/Error , // 輸出格式 format = mix/http/Error::FORMAT_JSON,],第三步
創(chuàng)建控制器:
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]; // 獲取數(shù)據(jù) $data = $model- getDetails(); if (!$data) { return [ code = ErrorCode::ERROR_ID_UNFOUND]; // 響應(yīng) return [ code = ErrorCode::SUCCESS, data = $data];}
創(chuàng)建錯(cuò)誤碼類:
apps/index/messages/ErrorCode.php
?phpnamespace apps/index/messages;class ErrorCode const SUCCESS = 0; const INVALID_PARAM = 100001; const ERROR_ID_UNFOUND = 200001;}
創(chuàng)建表單驗(yàn)證模型:
apps/index/models/ArticlesForm.php
?phpnamespace apps/index/models;use mix/validators/Validator;use apps/common/models/ArticlesModel;class ArticlesForm extends Validator public $id; // 規(guī)則 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- }
創(chuàng)建數(shù)據(jù)表模型:
apps/common/models/ArticlesModel.php
?phpnamespace apps/common/models;use mix/facades/RDB;class ArticlesModel const TABLE = articles // 獲取一行數(shù)據(jù)通過id public function getRowById($id) $sql = SELECT * FROM ` . self::TABLE . ` WHERE id = :id $row = RDB::createCommand($sql)- bindParams([ id = $id, ])- queryOne(); return $row;}
以上就是全部代碼的編寫。
第四步使用 Postman 測試,如下:
接口開發(fā)與測試完成,是不是很簡單呀。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請關(guān)注PHP !
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選