Skip to content

Commit 8b1bfa2

Browse files
committed
docs(readme): 📝 新增描述、安裝需求、安裝流程
1 parent 103629a commit 8b1bfa2

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

readme.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Laravel Plus API
2+
3+
Convenient Laravel API response tools and automated error handling functionality are provided.
4+
5+
## Requirement
6+
7+
- PHP ^7.4|^8.0
8+
- Laravel >= 7
9+
10+
## Installation
11+
12+
### 1. Composer install
13+
Run the Composer require command from the Terminal:
14+
```bash
15+
composer require hankz/laravel-plus-api
16+
```
17+
### 2. Setup
18+
This package supports Laravel's auto-discovery feature and it's ready to use once installed.
19+
20+
### 3. Publishing the config file
21+
You need publish the config file.
22+
```bash
23+
php artisan vendor:publish --provider="Hankz\LaravelPlusApi\LaravelPlusApiServiceProvider
24+
```
25+
26+
### 4. Set Middleware
27+
Set middleware for routes where you intend to utilize API responses.
28+
29+
in `app/Http/Kernel.php`
30+
```php
31+
'api' => [
32+
//...
33+
34+
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
35+
]
36+
```
37+
38+
Set the priority of the middleware.
39+
40+
in `app/Http/Kernel.php`
41+
```php
42+
protected $middlewarePriority = [
43+
\Illuminate\Session\Middleware\StartSession::class,
44+
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
45+
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
46+
\Hankz\LaravelPlusApi\Exceptions\ApiException::class,
47+
\App\Http\Middleware\Authenticate::class,
48+
\Illuminate\Routing\Middleware\ThrottleRequests::class,
49+
\Illuminate\Session\Middleware\AuthenticateSession::class,
50+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
51+
\Illuminate\Auth\Middleware\Authorize::class,
52+
];
53+
```
54+
55+
### 5. Exception Handler
56+
Override exception Handler.
57+
58+
in `app/Exceptions/Handler.php`,加入:
59+
```php
60+
use Illuminate\Auth\AuthenticationException;
61+
use Illuminate\Validation\ValidationException;
62+
use Symfony\Component\HttpFoundation\Response as HttpResponse;
63+
use Hankz\LaravelPlusApi\Classes\ApiResponseBuilder;
64+
65+
class Handler extends ExceptionHandler
66+
{
67+
use ApiResponse;
68+
69+
//...
70+
71+
protected function invalidJson($request, ValidationException $exception)
72+
{
73+
return ApiResponseBuilder::validationError($exception);
74+
}
75+
76+
protected function unauthenticated($request, AuthenticationException $exception)
77+
{
78+
return $this->error(config('plus-api.default_response.error.http_code'), Response::HTTP_UNAUTHORIZED);
79+
}
80+
81+
protected function prepareJsonResponse($request, Throwable $e)
82+
{
83+
return $this->exceptionError(
84+
$this->convertExceptionToArray($e),
85+
config('plus-api.default_response.error.http_code'),
86+
$this->isHttpException($e) ? $e->getStatusCode() : Response::HTTP_INTERNAL_SERVER_ERROR,
87+
$e->getMessage()
88+
$this->isHttpException($e) ? $e->getHeaders() : []
89+
);
90+
}
91+
```

0 commit comments

Comments
 (0)