Skip to content

Commit

Permalink
docs(readme): add support in documentation for Laravel 11
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosalpr committed Jun 28, 2024
1 parent 039eb51 commit a9fc7b3
Showing 1 changed file with 39 additions and 4 deletions.
43 changes: 39 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ You can install the package via composer:
composer require pedrosalpr/laravel-api-problem
```

> Only works in Laravel 9 and 10.
## Usage

### Default Mode
### Default Mode (Older Version Laravel 9 and 10)

To use it, just go to the `register` method within `Exceptions\Handler.php` and add the following code:

Expand All @@ -82,6 +80,26 @@ If you want to debug, just add the following line before the return:

`dd($apiProblem->toDebuggableArray());`

### Default Mode (Laravel 11)

To use it, add the following code to the Exception Closure in the `bootstrap/app.php` file:

```php
use Pedrosalpr\LaravelApiProblem\LaravelApiProblem;

...

->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(function (\Throwable $e, Request $request) {
if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
$apiProblem = new LaravelApiProblem($e, $request);
return $apiProblem->render();
}
});
})...

```

#### Creating Exceptions in the Api Problem pattern

There is the possibility of creating exceptions that extend `LaravelApiProblemException`.
Expand Down Expand Up @@ -162,7 +180,9 @@ class DummyApiProblem extends LaravelApiProblem

And within the match, add the names of the exceptions classes with their respective methods, such as `dummy()`.

And in the `Handler.php` file replace the `LaravelApiProblem` object instance to `DummyApiProblem`.
#### Older Version Laravel 9 and 10

In the `Handler.php` file replace the `LaravelApiProblem` object instance to `DummyApiProblem`.

```php
$this->renderable(function (\Throwable $e, Request $request) {
Expand All @@ -173,6 +193,21 @@ And in the `Handler.php` file replace the `LaravelApiProblem` object instance to
});
```

#### Laravel 11

Add the following code to the Exception Closure in the `bootstrap/app.php` file:

```php
->withExceptions(function (Exceptions $exceptions) {
$exceptions->render(function (\Throwable $e, Request $request) {
if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
$apiProblem = new DummyApiProblem($e, $request);
return $apiProblem->render();
}
});
})
```

## Testing

TODO
Expand Down

0 comments on commit a9fc7b3

Please sign in to comment.