前言
隨著開(kāi)發(fā)經(jīng)驗(yàn)的增加,也伴隨團(tuán)隊(duì)開(kāi)發(fā)的積累,規(guī)范開(kāi)發(fā)顯得越來(lái)越重要,本文給大家提供一些ThinkPHP框架開(kāi)發(fā)API接口的進(jìn)階思路和經(jīng)驗(yàn),讓大家開(kāi)發(fā)更加統(tǒng)一規(guī)范,代碼看起來(lái)更加優(yōu)雅。
1.更多使用第三方庫(kù)。團(tuán)隊(duì)開(kāi)發(fā)的時(shí)候,各個(gè)人的開(kāi)發(fā)經(jīng)驗(yàn)和思考方式不同,也有一些思維局限性,可以多多使用第三方庫(kù),統(tǒng)一代碼開(kāi)發(fā)規(guī)范。
2.拋棄傳統(tǒng)MVC模式,在結(jié)構(gòu)上分出更新的結(jié)構(gòu)分層,防止Controller過(guò)于臃腫和復(fù)雜,需要進(jìn)行代碼解耦。一般分為下面幾層:
-
數(shù)據(jù)驗(yàn)證層(Validate):負(fù)責(zé)驗(yàn)證請(qǐng)求參數(shù),單獨(dú)創(chuàng)建一個(gè)驗(yàn)證請(qǐng)求器,避免驗(yàn)證邏輯放在控制器中驗(yàn)證,例如:
Laravel框架使用FormRequest Laravel中使用FormRequest進(jìn)行表單驗(yàn)證及對(duì)驗(yàn)證異常進(jìn)行自定義處理_51CTO博客_laravel 表單驗(yàn)證
-
控制器層(Controller):負(fù)責(zé)接收參數(shù)、驗(yàn)證參數(shù)、調(diào)用各個(gè)模塊的服務(wù)層(可以用事務(wù)包裹,用到其他服務(wù)的可以注入多個(gè)服務(wù))、返回響應(yīng)、返回視圖等
-
服務(wù)層(Service):負(fù)責(zé)具體的業(yè)務(wù)邏輯實(shí)現(xiàn),將原本控制器的負(fù)責(zé)流程按模塊拆分為一個(gè)個(gè)小的服務(wù),方便給控制器層組合調(diào)用,一般不要跨模塊調(diào)用服務(wù),服務(wù)中可以調(diào)用本模塊的倉(cāng)庫(kù)層方法
-
倉(cāng)庫(kù)層(Repository):調(diào)用模型層封裝一些負(fù)責(zé)的查詢(xún),方便服務(wù)層調(diào)用,一般負(fù)責(zé)只查詢(xún)本模塊的內(nèi)容
-
模型層(Model):只包含默認(rèn)的表屬性(表名、字段等)和聯(lián)表關(guān)系
-
(更多層級(jí))
環(huán)境搭建
-
驗(yàn)證器(Validate)
ThinkPHP5.1及以上不需要使用第三方驗(yàn)證器擴(kuò)展,使用自帶的驗(yàn)證器即可,官方文檔:驗(yàn)證器 - ThinkPHP官方手冊(cè)
ThinkPHP5.0沒(méi)有提供命令行創(chuàng)建驗(yàn)證器,可以使用第三方擴(kuò)展包實(shí)現(xiàn):Thinkphp5.0命令行創(chuàng)建驗(yàn)證器validate類(lèi)-CSDN博客
-
服務(wù)層(Service)
使用第三方擴(kuò)展包增加命令行功能:Thinkphp命令行創(chuàng)建Service(服務(wù)層)擴(kuò)展包-CSDN博客
-
倉(cāng)庫(kù)層(Repository)
使用第三方擴(kuò)展包增加命令行功能:Thinkphp命令行創(chuàng)建repository和transform層擴(kuò)展包-CSDN博客
-
統(tǒng)一響應(yīng)數(shù)據(jù)結(jié)構(gòu)
增加自定義trait:Thinkphp封裝統(tǒng)一響應(yīng)-CSDN博客
開(kāi)發(fā)示例
用Think5.1框架以用戶(hù)注冊(cè)流程為例
-
控制器(Controller)
創(chuàng)建空白的用戶(hù)控制器
php think make:controller api/User --plain
控制器校驗(yàn)請(qǐng)求參數(shù)后調(diào)用不同 service 進(jìn)行業(yè)務(wù)處理,調(diào)用轉(zhuǎn)換器將數(shù)據(jù)返回,示例:
<?php namespace app\api\controller; use app\common\service\UserService; use app\common\transform\UserTransform; use app\common\validate\UserValidate; use think\App; use think\Db; class User extends Controller { protected $service; public function __construct(App $app = null, UserService $service) { parent::__construct($app); $this->service = $service; } /** * 獲取用戶(hù)信息 */ public function info(UserTransform $transform) { $userInfo = $this->service->getUserById(1); $this->success($transform->userInfo($userInfo)); //使用轉(zhuǎn)化器 } /** * 用戶(hù)注冊(cè) * * @param UserValidate $validate 用戶(hù)驗(yàn)證器 * @param SmsService $smsService 短信服務(wù) * @return void */ public function register(UserValidate $validate, SmsService $smsService){ //參數(shù)驗(yàn)證 $data = $this->request->param(); if (!$validate->scene('register')->check($data)) { $this->fail($validate->getError()); // 繼承的Controller里use trait CustomResponse,所以這里可以直接使用fail方法; } //短信驗(yàn)證碼驗(yàn)證 if (!$smsService->check($data['mobile'], 'register', $data['sms_code'])){ $this->fail('驗(yàn)證碼錯(cuò)誤'); } Db::startTrans(); try{ //新增用戶(hù) $this->userService->addUser($data); //綁定推薦人... //其他服務(wù)操作... // 提交事務(wù) Db::commit(); }catch (\Exception $e){ // 回滾事務(wù) Db::rollback(); $this->error($e->getMessage()); } $this->ok('注冊(cè)成功'); } }
-
驗(yàn)證器(Validate)
創(chuàng)建驗(yàn)證器
php think make:validate UserValidate
在驗(yàn)證器中添加驗(yàn)證規(guī)則、提示信息和驗(yàn)證場(chǎng)景
<?php namespace app\common\validate; use think\Validate; class UserValidate extends Validate { /** * 定義驗(yàn)證規(guī)則 * 格式:'字段名' => ['規(guī)則1','規(guī)則2'...] * * @var array */ protected $rule = [ 'mobile' => 'require|regex:/^1[3-9]\d{9}$/', 'password' => 'require|min:6', 'sms_code' => 'require|length:6|number', ]; /** * 定義錯(cuò)誤信息 * 格式:'字段名.規(guī)則名' => '錯(cuò)誤信息' * * @var array */ protected $message = [ 'mobile.require' => '手機(jī)號(hào)碼必填', 'mobile.regex' => '手機(jī)號(hào)碼格式不正確', 'password.require' => '密碼不能為空', 'sms_code.require' => '短信驗(yàn)證碼不能為空', 'sms_code.length' => '請(qǐng)輸入6位短信驗(yàn)證碼', ]; /** * 定義驗(yàn)證場(chǎng)景和對(duì)應(yīng)的驗(yàn)證字段 */ protected $scene = [ //注冊(cè) 'register' => [ 'mobile', 'password', 'sms_code' ], //登錄 'login' => [ 'mobile', 'password' ], ]; }
-
模型(Model)
創(chuàng)建模型
php think make:model User
User模型的默認(rèn)內(nèi)容為
<?php namespace app\common\model; use think\Model; class User extends Model { // }
-
服務(wù)層(Service)
創(chuàng)建服務(wù)
php think make:service UserService
生成的
UserService
自動(dòng)綁定對(duì)應(yīng)的倉(cāng)庫(kù)<?php namespace app\common\service; use app\common\repository\UserRepository; class UserService { /** * 綁定倉(cāng)庫(kù) * @var UserRepository */ protected $repository; public function __construct(UserRepository $repository) { $this->repository = $repository; } /** * 添加用戶(hù) */ public function addUser($data) { //密碼加密、用戶(hù)數(shù)據(jù)處理 ... //創(chuàng)建用戶(hù) $this->repository->create($data); } /** * 查找用戶(hù) * @param $id * @return \Illuminate\Database\Eloquent\Builder */ public function getUserById($id) { return $this->repository->find($id); } }
-
倉(cāng)庫(kù)層(Repository)
創(chuàng)建倉(cāng)庫(kù)
php think make:repository UserRepository
生成的
UserRepository
自動(dòng)綁定對(duì)應(yīng)的模型,里面定義一些聯(lián)表等復(fù)雜查詢(xún)操作<?php namespace app\common\repository; use think\App; use app\common\model\User; use Jian1098\TpRepository\Repository; /** * Class UserRepository */ class UserRepository extends Repository { protected $model; public function __construct() { parent::__construct(new App()); //綁定模型 $this->model = new User(); } }
-
轉(zhuǎn)換器
轉(zhuǎn)換器用于轉(zhuǎn)換查詢(xún)到的數(shù)據(jù)結(jié)果接口,可用可不用,如果需要對(duì)多個(gè)字段進(jìn)行處理,使用轉(zhuǎn)換器比較美觀優(yōu)雅
創(chuàng)建轉(zhuǎn)換器文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-860186.html
php think make:transform UserTransform
默認(rèn)內(nèi)容如下文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-860186.html
<?php namespace app\common\transform; use Jian1098\TpRepository\Command\Transform; class UserTransform extends Transform { //用戶(hù)信息 public function userInfo($items) { return [ 'user_id' => $items['id'], 'user_name' => $items['nickname'], ]; } }
到了這里,關(guān)于讓php開(kāi)發(fā)更優(yōu)雅-ThinkPHP篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!