|
| 1 | +## Installation |
| 2 | + |
| 3 | +### 1. Composer install |
| 4 | + |
| 5 | +Run the Composer require command from the Terminal: |
| 6 | + |
| 7 | +```bash |
| 8 | +composer require hankz/laravel-plus-api |
| 9 | +``` |
| 10 | + |
| 11 | +### 2. Setup |
| 12 | + |
| 13 | +This package supports Laravel's auto-discovery feature and it's ready to use once installed. |
| 14 | + |
| 15 | +### 3. Publishing the config file |
| 16 | + |
| 17 | +You need publish the config file. |
| 18 | + |
| 19 | +```bash |
| 20 | +php artisan vendor:publish --provider="Hankz\LaravelPlusApi\LaravelPlusApiServiceProvider" |
| 21 | +``` |
| 22 | + |
| 23 | +### 4. Set Middleware |
| 24 | + |
| 25 | +Set middleware for routes where you intend to utilize API responses. |
| 26 | + |
| 27 | +in `bootstrap/app.php` |
| 28 | + |
| 29 | +```php |
| 30 | +->withMiddleware(function (Middleware $middleware) { |
| 31 | + $middleware->api(append:[ |
| 32 | + \Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class, |
| 33 | + ]); |
| 34 | + |
| 35 | + $middleware->priority([ |
| 36 | + \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class, |
| 37 | + \Illuminate\Cookie\Middleware\EncryptCookies::class, |
| 38 | + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, |
| 39 | + \Illuminate\Session\Middleware\StartSession::class, |
| 40 | + \Illuminate\View\Middleware\ShareErrorsFromSession::class, |
| 41 | + \Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class, |
| 42 | + \Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class, |
| 43 | + \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, |
| 44 | + \Illuminate\Routing\Middleware\ThrottleRequests::class, |
| 45 | + \Illuminate\Routing\Middleware\ThrottleRequestsWithRedis::class, |
| 46 | + \Illuminate\Routing\Middleware\SubstituteBindings::class, |
| 47 | + \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class, |
| 48 | + \Illuminate\Auth\Middleware\Authorize::class, |
| 49 | + ]); |
| 50 | +}) |
| 51 | +``` |
| 52 | + |
| 53 | +### 5. Exception Handler |
| 54 | + |
| 55 | +Set exception Handler. |
| 56 | + |
| 57 | +in `bootstrap/app.php`,add: |
| 58 | + |
| 59 | +```php |
| 60 | +->withExceptions(function (Exceptions $exceptions) { |
| 61 | + $exceptions->render(function (Throwable $throwable, Request $request) { |
| 62 | + |
| 63 | + if ($request->is('api/*')) { // When your API route prefix is not "api", remember to modify it. |
| 64 | + |
| 65 | + if ($throwable instanceof ValidationException) { |
| 66 | + return ApiResponseBuilder::validationError($throwable); |
| 67 | + } |
| 68 | + |
| 69 | + if ($throwable instanceof AuthenticationException) { |
| 70 | + return ApiResponseBuilder::error( |
| 71 | + config('laravel-plus-api.default_response.unauthenticated.api_code'), |
| 72 | + config('laravel-plus-api.default_response.unauthenticated.http_code'), |
| 73 | + config('laravel-plus-api.default_response.unauthenticated.message') |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + $defaultResponseConfig = config('laravel-plus-api.default_response.error'); |
| 78 | + |
| 79 | + return ApiResponseBuilder::exceptionError( |
| 80 | + $throwable, |
| 81 | + data_get($defaultResponseConfig, 'api_code'), |
| 82 | + ($throwable instanceof HttpExceptionInterface) ? $throwable->getStatusCode() : data_get($defaultResponseConfig, 'http_code'), |
| 83 | + $throwable->getMessage() ? $throwable->getMessage() : data_get($defaultResponseConfig, 'message'), |
| 84 | + null, |
| 85 | + ($throwable instanceof HttpExceptionInterface) ? $throwable->getHeaders() : [] |
| 86 | + ); |
| 87 | + } |
| 88 | + }); |
| 89 | +}) |
| 90 | +``` |
0 commit comments