下面由Laravel教程欄目給大家介紹如何把開發(fā)中常用class的整合成了一個包,避免每次重復(fù)復(fù)制粘貼的方法,希望對需要的朋友有所幫助!
laravel-quick
laravel-quick(github 地址:https://github.com/youyingxiang/laravel-quick.git) 封裝了一些我們開發(fā)中常見的工具,使開發(fā)變得更高效
- 主要包含翻譯了驗(yàn)證的語言包提示
- 根據(jù) SymfonyComponentHttpFoundationResponse 為狀態(tài)碼的接口格式
- 異常類處理
- 集成基于 redis 的各種緩存操作
- service,repository,trait的 artisan 命令生成;
安裝
composer require yxx/laravel-quick
- linux 和 mac
php artisan vendor:publish --provider="Yxx\LaravelQuick\LaravelQuickServiceProvider"
- windows
php artisan vendor:publish --provider="YxxLaravelQuickLaravelQuickServiceProvider"
怎么使用
- 異常使用例子
use YxxLaravelQuickExceptionsApiApiNotFoundException;// 請求參數(shù)錯誤throw new ApiRequestException();// 404 未找到throw new ApiNotFoundException();// 系統(tǒng)錯誤throw new ApiSystemException()// 未授權(quán)throw new ApiUnAuthException()自定義錯誤繼承YxxLaravelQuickExceptions自己參照對應(yīng)代碼自定義
- api 接口使用
use YxxLaravelQuickTraitsJsonResponseTrait// 成功return $this->success("消息",['name'=>"張三"]);// 失敗return $this->error("錯誤");// 自定義return $this->apiResponse(Response::HTTP_BAD_GATEWAY,"502錯誤");
- 緩存的使用(封裝了 redis 的一些方法)
use YxxLaravelQuickFacadesCacheClient;CacheClient::hSet("test","1","張三");CacheClient::hGet("test","1");CacheClient::lPush("test","1");具體參考YxxLaravelQuickServicesCacheService里面的方法....
artisan 命令
- 創(chuàng)建 Trait
php artisan quick:create-trait test
- 創(chuàng)建 Service
php artisan quick:create-service Test/TestService
- 創(chuàng)建 Repository
php artisan quick:create-repository Test