把laravel开发中常用class的整合成一个包,避免每次重复复制粘贴
下面由Laravel教程欄目給大家介紹如何把開發(fā)中常用class的整合成了一個包,避免每次重復復制粘貼的方法,希望對需要的朋友有所幫助!
laravel-quick
laravel-quick(github 地址:https://github.com/youyingxiang/laravel-quick.git) 封裝了一些我們開發(fā)中常見的工具,使開發(fā)變得更高效
主要包含翻譯了驗證的語言包提示根據(jù) Symfony\\Component\\HttpFoundation\\Response 為狀態(tài)碼的接口格式異常類處理集成基于 redis 的各種緩存操作service,repository,trait的 artisan 命令生成;安裝composer require yxx/laravel-quicklinux 和 macphp artisan vendor:publish --provider="Yxx\\\\LaravelQuick\\\\LaravelQuickServiceProvider"windowsphp artisan vendor:publish --provider="Yxx\\LaravelQuick\\LaravelQuickServiceProvider"怎么使用異常使用例子
use Yxx\\LaravelQuick\\Exceptions\\Api\\ApiNotFoundException;// 請求參數(shù)錯誤throw new ApiRequestException();// 404 未找到throw new ApiNotFoundException();// 系統(tǒng)錯誤throw new ApiSystemException()// 未授權throw new ApiUnAuthException()自定義錯誤繼承Yxx\\LaravelQuick\\Exceptions自己參照對應代碼自定義
api 接口使用
use Yxx\\LaravelQuick\\Traits\\JsonResponseTrait// 成功return $this->success("消息",['name'=>"張三"]);// 失敗return $this->error("錯誤");// 自定義return $this->apiResponse(Response::HTTP_BAD_GATEWAY,"502錯誤");
緩存的使用(封裝了 redis 的一些方法)
use Yxx\\LaravelQuick\\Facades\\CacheClient;CacheClient::hSet("test","1","張三");CacheClient::hGet("test","1");CacheClient::lPush("test","1");具體參考Yxx\\LaravelQuick\\Services\\CacheService里面的方法....
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
總結(jié)
以上是生活随笔為你收集整理的把laravel开发中常用class的整合成一个包,避免每次重复复制粘贴的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信商城真有那么好吗
- 下一篇: 打破私域流量增长困局,需要这三大关键力!