Skip to content

Commit baa78fa

Browse files
authored
Merge pull request #6 from hankz1108/main
Release v0.2.0
2 parents a1a17b8 + 83f6707 commit baa78fa

File tree

10 files changed

+463
-298
lines changed

10 files changed

+463
-298
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Hankz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^7.4|^8.0",
25-
"laravel/framework": ">=7"
25+
"laravel/framework": "^7.0|^8.0|^9.0|^10.0|^11.0"
2626
},
2727
"extra": {
2828
"laravel": {

docs/en/11/index.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
```

docs/en/7_10/index.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 `app/Http/Kernel.php`
28+
29+
```php
30+
'api' => [
31+
//...
32+
33+
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
34+
]
35+
```
36+
37+
Set the priority of the middleware.
38+
39+
in `app/Http/Kernel.php`
40+
41+
```php
42+
protected $middlewarePriority = [
43+
\Illuminate\Session\Middleware\StartSession::class,
44+
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
45+
\Hankz\LaravelPlusApi\Middleware\SetHeaderAcceptJson::class,
46+
\App\Http\Middleware\Authenticate::class,
47+
\Illuminate\Routing\Middleware\ThrottleRequests::class,
48+
\Illuminate\Session\Middleware\AuthenticateSession::class,
49+
\Illuminate\Routing\Middleware\SubstituteBindings::class,
50+
\Illuminate\Auth\Middleware\Authorize::class,
51+
];
52+
```
53+
54+
### 5. Exception Handler
55+
56+
Override exception Handler.
57+
58+
in `app/Exceptions/Handler.php`,add:
59+
60+
```php
61+
use Hankz\LaravelPlusApi\Classes\ApiResponseBuilder;
62+
63+
class Handler extends ExceptionHandler
64+
{
65+
//...
66+
67+
protected function invalidJson($request, ValidationException $exception)
68+
{
69+
return ApiResponseBuilder::validationError($exception);
70+
}
71+
72+
protected function unauthenticated($request, AuthenticationException $exception)
73+
{
74+
return ApiResponseBuilder::error(
75+
config('laravel-plus-api.default_response.unauthenticated.api_code'),
76+
config('laravel-plus-api.default_response.unauthenticated.http_code'),
77+
config('laravel-plus-api.default_response.unauthenticated.message')
78+
);
79+
}
80+
81+
protected function prepareJsonResponse($request, Throwable $e)
82+
{
83+
return ApiResponseBuilder::exceptionError(
84+
$throwable,
85+
data_get($defaultResponseConfig, 'api_code'),
86+
($throwable instanceof HttpExceptionInterface) ? $throwable->getStatusCode() : data_get($defaultResponseConfig, 'http_code'),
87+
$throwable->getMessage() ? $throwable->getMessage() : data_get($defaultResponseConfig, 'message'),
88+
null,
89+
($throwable instanceof HttpExceptionInterface) ? $throwable->getHeaders() : []
90+
);
91+
}
92+
```

readme.md

Lines changed: 7 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,14 @@
11
# Laravel Plus API
22

3-
Convenient Laravel API response tools and automated error handling functionality are provided.
4-
3+
Convenient Laravel API response tools and automated error handling functionality are provided.
4+
Laravel Plus API` is inspired by the [marcin-orlowski/laravel-api-response-builder](https://github.com/MarcinOrlowski/laravel-api-response-builder) with adjustments made to its features.
55
## Requirement
6-
76
- PHP ^7.4|^8.0
8-
- Laravel >= 7
7+
- Laravel >= ^7.0|^8.0|^9.0|^10.0|^11.0
98

109
## Installation
10+
### For Laravel 7~10
11+
[documentation](docs/en/7_10/index.md)
1112

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-
```
13+
### For Laravel 11
14+
[documentation](docs/en/11/index.md)

0 commit comments

Comments
 (0)