|
| 1 | +<h1 style="text-align: center;"> support </h1> |
| 2 | + |
| 3 | +## 安装 |
| 4 | + |
| 5 | +```shell |
| 6 | +$ composer require zhenmu/support -vvv |
| 7 | +``` |
| 8 | + |
| 9 | +## 使用 |
| 10 | + |
| 11 | +1. 通过 `php artisan make:controller` 控制器生成后,继承同目录下的 `Controller` 基类. |
| 12 | +2. 编写接口时可通过 `$this->success($data = [], $err_code = 200, $messsage = 'success');` 返回正确数据给接口. |
| 13 | +3. 编写接口时可通过 `$this->fail($messsage = '', $err_code = 400);` 返回错误信息给接口. |
| 14 | +4. 在 `app/Exceptions/Handler.php` 的 `register` 函数中, 注册 `ResponseTrait` 的 `renderableHandle`, 示例见下方错误处理. |
| 15 | + |
| 16 | + |
| 17 | +### 控制器 |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | + |
| 22 | +namespace App\Http\Controllers; |
| 23 | + |
| 24 | +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
| 25 | +use Illuminate\Foundation\Bus\DispatchesJobs; |
| 26 | +use Illuminate\Foundation\Validation\ValidatesRequests; |
| 27 | +use Illuminate\Routing\Controller as BaseController; |
| 28 | +use ZhenMu\Support\Traits\ResponseTrait; // here |
| 29 | + |
| 30 | +class Controller extends BaseController |
| 31 | +{ |
| 32 | + use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
| 33 | + use ResponseTrait; // here |
| 34 | +} |
| 35 | + |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +### 错误处理 |
| 40 | + |
| 41 | +```php |
| 42 | +<?php |
| 43 | + |
| 44 | +namespace App\Exceptions; |
| 45 | + |
| 46 | +use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
| 47 | +use Throwable; |
| 48 | +use ZhenMu\Support\Traits\ResponseTrait; // here |
| 49 | + |
| 50 | +class Handler extends ExceptionHandler |
| 51 | +{ |
| 52 | + use ResponseTrait; // here |
| 53 | + |
| 54 | + /** |
| 55 | + * A list of exception types with their corresponding custom log levels. |
| 56 | + * |
| 57 | + * @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*> |
| 58 | + */ |
| 59 | + protected $levels = [ |
| 60 | + // |
| 61 | + ]; |
| 62 | + |
| 63 | + /** |
| 64 | + * A list of the exception types that are not reported. |
| 65 | + * |
| 66 | + * @var array<int, class-string<\Throwable>> |
| 67 | + */ |
| 68 | + protected $dontReport = [ |
| 69 | + // |
| 70 | + ]; |
| 71 | + |
| 72 | + /** |
| 73 | + * A list of the inputs that are never flashed for validation exceptions. |
| 74 | + * |
| 75 | + * @var array<int, string> |
| 76 | + */ |
| 77 | + protected $dontFlash = [ |
| 78 | + 'current_password', |
| 79 | + 'password', |
| 80 | + 'password_confirmation', |
| 81 | + ]; |
| 82 | + |
| 83 | + /** |
| 84 | + * Register the exception handling callbacks for the application. |
| 85 | + * |
| 86 | + * @return void |
| 87 | + */ |
| 88 | + public function register() |
| 89 | + { |
| 90 | + $this->reportable(function (Throwable $e) { |
| 91 | + // |
| 92 | + }); |
| 93 | + |
| 94 | + $this->renderable($this->renderableHandle()); // here |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +``` |
| 99 | + |
| 100 | +## 控制器调用 |
| 101 | + |
| 102 | +``` |
| 103 | +<?php |
| 104 | +
|
| 105 | +namespace App\Http\Controllers; |
| 106 | +
|
| 107 | +use Illuminate\Http\Request; |
| 108 | +
|
| 109 | +class DemoController extends Controller |
| 110 | +{ |
| 111 | + public function index() |
| 112 | + { |
| 113 | + // validate data |
| 114 | + \validator()->validate(\request(), [ |
| 115 | + 'name' => 'required|string', |
| 116 | + 'age' => 'nullable|integer', |
| 117 | + ]); |
| 118 | +
|
| 119 | + // your business logic |
| 120 | + $error = false; |
| 121 | + if ($error) { // here business logic error. |
| 122 | + throw new \RuntimeException('error message'); |
| 123 | + } |
| 124 | +
|
| 125 | + return $this->success([ // here response success |
| 126 | + 'key1' => 'value1', |
| 127 | + 'key2' => 'value2', |
| 128 | + ]); |
| 129 | + } |
| 130 | +} |
| 131 | +
|
| 132 | +``` |
0 commit comments