把laravel開發中常用class的整合成一個包,避免每次重復復制粘貼

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

下面由laravel教程欄目給大家介紹如何把開發中常用class的整合成了一個包,避免每次重復復制粘貼的方法,希望對需要的朋友有所幫助!

laravel-quick

laravel-quick(github 地址:https://github.com/youyingxiang/laravel-quick.git) 封裝了一些我們開發中常見的工具,使開發變得更高效

  • 主要包含翻譯了驗證的語言包提示
  • 根據 SymfonyComponentHttpFoundationResponse 為狀態碼的接口格式
  • 異常類處理
  • 集成基于 redis 的各種緩存操作
  • service,repository,trait的 artisan 命令生成;

安裝

  • composer require yxx/laravel-quick
  • linux 和 mac
    php artisan vendor:publish –provider=”YxxLaravelQuickLaravelQuickServiceProvider”
  • windows
    php artisan vendor:publish –provider=”YxxLaravelQuickLaravelQuickServiceProvider”

怎么使用

  • 異常使用例子
    use YxxLaravelQuickExceptionsApiApiNotFoundException;// 請求參數錯誤throw new ApiRequestException();// 404 未找到throw new ApiNotFoundException();// 系統錯誤throw new ApiSystemException()// 未授權throw new ApiUnAuthException()自定義錯誤繼承YxxLaravelQuickExceptions自己參照對應代碼自定義
  • 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 命令

  • 創建 Trait php artisan quick:create-trait test
  • 創建 Service ?php artisan quick:create-service Test/TestService
  • 創建 Repository php artisan quick:create-repository Test

以上就是把

? 版權聲明
THE END
喜歡就支持一下吧
點贊10 分享