首頁  >  文章  >  php框架  >  把laravel開發中常用class的整合成一個包,避免每次重複複製貼上

把laravel開發中常用class的整合成一個包,避免每次重複複製貼上

藏色散人
藏色散人轉載
2020-09-17 09:22:102162瀏覽

下面由Laravel教學專欄給大家介紹如何把開發中常用class的整合成了一個包,避免每次重複複製貼上的方法,希望對需要的朋友有幫助!

laravel-quick

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

  • 主要包含翻譯了驗證的語言包提示
  • #根據Symfony\Component\HttpFoundation\Response 為狀態碼的介面格式
  • 異常類別處理
  • 整合基於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 ="Yxx\LaravelQuick\LaravelQuickServiceProvider"

怎麼使用

  • 異常使用範例
    use Yxx\LaravelQuick\Exceptions\Api\ApiNotFoundException;// 请求参数错误throw new ApiRequestException();// 404 未找到throw new ApiNotFoundException();// 系统错误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 指令

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

以上是把laravel開發中常用class的整合成一個包,避免每次重複複製貼上的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除