@@ -54,11 +54,9 @@ You can install the package via composer:
5454composer require pedrosalpr/laravel-api-problem
5555```
5656
57- > Only works in Laravel 9 and 10.
58-
5957## Usage
6058
61- ### Default Mode
59+ ### Default Mode (Older Version Laravel 9 and 10)
6260
6361To use it, just go to the ` register ` method within ` Exceptions\Handler.php ` and add the following code:
6462
@@ -82,6 +80,26 @@ If you want to debug, just add the following line before the return:
8280
8381` dd($apiProblem->toDebuggableArray()); `
8482
83+ ### Default Mode (Laravel 11)
84+
85+ To use it, add the following code to the Exception Closure in the ` bootstrap/app.php ` file:
86+
87+ ``` php
88+ use Pedrosalpr\LaravelApiProblem\LaravelApiProblem;
89+
90+ ...
91+
92+ ->withExceptions(function (Exceptions $exceptions) {
93+ $exceptions->render(function (\Throwable $e, Request $request) {
94+ if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
95+ $apiProblem = new LaravelApiProblem($e, $request);
96+ return $apiProblem->render();
97+ }
98+ });
99+ })...
100+
101+ ```
102+
85103#### Creating Exceptions in the Api Problem pattern
86104
87105There is the possibility of creating exceptions that extend ` LaravelApiProblemException ` .
@@ -162,7 +180,9 @@ class DummyApiProblem extends LaravelApiProblem
162180
163181And within the match, add the names of the exceptions classes with their respective methods, such as ` dummy() ` .
164182
165- And in the ` Handler.php ` file replace the ` LaravelApiProblem ` object instance to ` DummyApiProblem ` .
183+ #### Older Version Laravel 9 and 10
184+
185+ In the ` Handler.php ` file replace the ` LaravelApiProblem ` object instance to ` DummyApiProblem ` .
166186
167187``` php
168188 $this->renderable(function (\Throwable $e, Request $request) {
@@ -173,6 +193,21 @@ And in the `Handler.php` file replace the `LaravelApiProblem` object instance to
173193 });
174194```
175195
196+ #### Laravel 11
197+
198+ Add the following code to the Exception Closure in the ` bootstrap/app.php ` file:
199+
200+ ``` php
201+ ->withExceptions(function (Exceptions $exceptions) {
202+ $exceptions->render(function (\Throwable $e, Request $request) {
203+ if ($request->is('api/*') || $this->shouldReturnJson($request, $e)) {
204+ $apiProblem = new DummyApiProblem($e, $request);
205+ return $apiProblem->render();
206+ }
207+ });
208+ })
209+ ```
210+
176211## Testing
177212
178213TODO
0 commit comments