Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 200 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
# Ignition: a beautiful error page for PHP apps
2
 
3
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/ignition.svg?style=flat-square)](https://packagist.org/packages/spatie/ignition)
4
[![Run tests](https://github.com/spatie/ignition/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/ignition/actions/workflows/run-tests.yml)
5
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/ignition.svg?style=flat-square)](https://packagist.org/packages/spatie/ignition)
6
 
7
[Ignition](https://flareapp.io/docs/ignition-for-laravel/introduction) is a beautiful and customizable error page for
8
PHP applications
9
 
10
Here's a minimal example on how to register ignition.
11
 
12
```php
13
use Spatie\Ignition\Ignition;
14
 
15
include 'vendor/autoload.php';
16
 
17
Ignition::make()->register();
18
```
19
 
20
Let's now throw an exception during a web request.
21
 
22
```php
23
throw new Exception('Bye world');
24
```
25
 
26
This is what you'll see in the browser.
27
 
28
![Screenshot of ignition](https://spatie.github.io/ignition/ignition.png)
29
 
30
There's also a beautiful dark mode.
31
 
32
![Screenshot of ignition in dark mode](https://spatie.github.io/ignition/ignition-dark.png)
33
 
34
## Are you a visual learner?
35
 
36
In [this video on YouTube](https://youtu.be/LEY0N0Bteew?t=739), you'll see a demo of all of the features.
37
 
38
Do know more about the design decisions we made, read [this blog post](https://freek.dev/2168-ignition-the-most-beautiful-error-page-for-laravel-and-php-got-a-major-redesign).
39
 
40
## Support us
41
 
42
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/ignition.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/ignition)
43
 
44
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can
45
support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
46
 
47
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
48
You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards
49
on [our virtual postcard wall](https://spatie.be/open-source/postcards).
50
 
51
## Installation
52
 
53
For Laravel apps, head over to [laravel-ignition](https://github.com/spatie/laravel-ignition).
54
 
55
For Symfony apps, go to [symfony-ignition-bundle](https://github.com/spatie/symfony-ignition-bundle).
56
 
57
For all other PHP projects, install the package via composer:
58
 
59
```bash
60
composer require spatie/ignition
61
```
62
 
63
## Usage
64
 
65
In order to display the Ignition error page when an error occurs in your project, you must add this code. Typically, this would be done in the bootstrap part of your application.
66
 
67
```php
68
\Spatie\Ignition\Ignition::make()->register();
69
```
70
 
71
### Setting the application path
72
 
73
When setting the application path, Ignition will trim the given value from all paths. This will make the error page look
74
more cleaner.
75
 
76
```php
77
\Spatie\Ignition\Ignition::make()
78
    ->applicationPath($basePathOfYourApplication)
79
    ->register();
80
```
81
 
82
### Using dark mode
83
 
84
By default, Ignition uses a nice white based theme. If this is too bright for your eyes, you can use dark mode.
85
 
86
```php
87
\Spatie\Ignition\Ignition::make()
88
    ->useDarkMode()
89
    ->register();
90
```
91
 
92
### Avoid rendering Ignition in a production environment
93
 
94
You don't want to render the Ignition error page in a production environment, as it potentially can display sensitive
95
information.
96
 
97
To avoid rendering Ignition, you can call `shouldDisplayException` and pass it a falsy value.
98
 
99
```php
100
\Spatie\Ignition\Ignition::make()
101
    ->shouldDisplayException($inLocalEnvironment)
102
    ->register();
103
```
104
 
105
### Displaying solutions
106
 
107
In addition to displaying an exception, Ignition can display a solution as well.
108
 
1294 lars 109
Out of the box, Ignition will display solutions for common errors such as bad methods calls, or using undefined properties.
148 lars 110
 
111
#### Adding a solution directly to an exception
112
 
113
To add a solution text to your exception, let the exception implement the `Spatie\Ignition\Contracts\ProvidesSolution`
114
interface.
115
 
116
This interface requires you to implement one method, which is going to return the `Solution` that users will see when
117
the exception gets thrown.
118
 
119
```php
120
use Spatie\Ignition\Contracts\Solution;
121
use Spatie\Ignition\Contracts\ProvidesSolution;
122
 
123
class CustomException extends Exception implements ProvidesSolution
124
{
125
    public function getSolution(): Solution
126
    {
127
        return new CustomSolution();
128
    }
129
}
130
```
131
 
132
```php
133
use Spatie\Ignition\Contracts\Solution;
134
 
135
class CustomSolution implements Solution
136
{
137
    public function getSolutionTitle(): string
138
    {
139
        return 'The solution title goes here';
140
    }
141
 
142
    public function getSolutionDescription(): string
143
    {
144
        return 'This is a longer description of the solution that you want to show.';
145
    }
146
 
147
    public function getDocumentationLinks(): array
148
    {
149
        return [
150
            'Your documentation' => 'https://your-project.com/relevant-docs-page',
151
        ];
152
    }
153
}
154
```
155
 
156
This is how the exception would be displayed if you were to throw it.
157
 
158
![Screenshot of solution](https://spatie.github.io/ignition/solution.png)
159
 
160
#### Using solution providers
161
 
162
Instead of adding solutions to exceptions directly, you can also create a solution provider. While exceptions that
163
return a solution, provide the solution directly to Ignition, a solution provider allows you to figure out if an
164
exception can be solved.
165
 
166
For example, you could create a custom "Stack Overflow solution provider", that will look up if a solution can be found
167
for a given throwable.
168
 
169
Solution providers can be added by third party packages or within your own application.
170
 
171
A solution provider is any class that implements the \Spatie\Ignition\Contracts\HasSolutionsForThrowable interface.
172
 
173
This is how the interface looks like:
174
 
175
```php
176
interface HasSolutionsForThrowable
177
{
178
    public function canSolve(Throwable $throwable): bool;
179
 
180
    /** @return \Spatie\Ignition\Contracts\Solution[] */
181
    public function getSolutions(Throwable $throwable): array;
182
}
183
```
184
 
185
When an error occurs in your app, the class will receive the `Throwable` in the `canSolve` method. In that method you
186
can decide if your solution provider is applicable to the `Throwable` passed. If you return `true`, `getSolutions` will
187
get called.
188
 
189
To register a solution provider to Ignition you must call the `addSolutionProviders` method.
190
 
191
```php
192
\Spatie\Ignition\Ignition::make()
193
    ->addSolutionProviders([
194
        YourSolutionProvider::class,
195
        AnotherSolutionProvider::class,
196
    ])
197
    ->register();
198
```
199
 
1294 lars 200
### AI powered solutions
201
 
202
Ignition can send your exception to Open AI that will attempt to automatically suggest a solution. In many cases, the suggested solutions is quite useful, but keep in mind that the solution may not be 100% correct for your context.
203
 
204
To generate AI powered solutions, you must first install this optional dependency.
205
 
206
```bash
207
composer require openai-php/client
208
```
209
 
210
To start sending your errors to OpenAI, you must instanciate the `OpenAiSolutionProvider`. The constructor expects a OpenAI API key to be passed, you should generate this key [at OpenAI](https://platform.openai.com).
211
 
212
```php
213
use \Spatie\Ignition\Solutions\OpenAi\OpenAiSolutionProvider;
214
 
215
$aiSolutionProvider = new OpenAiSolutionProvider($openAiKey);
216
```
217
 
218
To use the solution provider, you should pass it to `addSolutionProviders` when registering Ignition.
219
 
220
```php
221
\Spatie\Ignition\Ignition::make()
222
    ->addSolutionProviders([
223
        $aiSolutionProvider,
224
        // other solution providers...
225
    ])
226
    ->register();
227
```
228
 
229
By default, the solution provider will send these bits of info to Open AI:
230
 
231
- the error message
232
- the error class
233
- the stack frame
234
- other small bits of info of context surrounding your error
235
 
236
It will not send the request payload or any environment variables to avoid sending sensitive data to OpenAI.
237
 
238
#### Caching requests to AI
239
 
240
By default, all errors will be sent to OpenAI. Optionally, you can add caching so similar errors will only get sent to OpenAI once. To cache errors, you can call `useCache` on `$aiSolutionProvider`. You should pass [a simple-cache-implementation](https://packagist.org/providers/psr/simple-cache-implementation). Here's the signature of the `useCache` method.
241
 
242
```php
243
public function useCache(CacheInterface $cache, int $cacheTtlInSeconds = 60 * 60)
244
```
245
 
246
#### Hinting the application type
247
 
248
To increase the quality of the suggested solutions, you can send along the application type (Symfony, Drupal, WordPress, ...) to the AI.
249
 
250
To send the application type call `applicationType` on the solution provider.
251
 
252
```php
253
$aiSolutionProvider->applicationType('WordPress 6.2')
254
```
255
 
148 lars 256
### Sending exceptions to Flare
257
 
258
Ignition comes with the ability to send exceptions to [Flare](https://flareapp.io), an exception monitoring service. Flare
259
can notify you when new exceptions are occurring in your production environment.
260
 
261
To send exceptions to Flare, simply call the `sendToFlareMethod` and pass it the API key you got when creating a project
262
on Flare.
263
 
264
You probably want to combine this with calling `runningInProductionEnvironment`. That method will, when passed a truthy
265
value, not display the Ignition error page, but only send the exception to Flare.
266
 
267
```php
268
\Spatie\Ignition\Ignition::make()
269
    ->runningInProductionEnvironment($boolean)
270
    ->sendToFlare($yourApiKey)
271
    ->register();
272
```
273
 
274
When you pass a falsy value to `runningInProductionEnvironment`, the Ignition error page will get shown, but no
275
exceptions will be sent to Flare.
276
 
277
### Sending custom context to Flare
278
 
279
When you send an error to Flare, you can add custom information that will be sent along with every exception that
280
happens in your application. This can be very useful if you want to provide key-value related information that
281
furthermore helps you to debug a possible exception.
282
 
283
```php
284
use Spatie\FlareClient\Flare;
285
 
286
\Spatie\Ignition\Ignition::make()
287
    ->runningInProductionEnvironment($boolean)
288
    ->sendToFlare($yourApiKey)
289
    ->configureFlare(function(Flare  $flare) {
290
        $flare->context('Tenant', 'My-Tenant-Identifier');
291
    })
292
    ->register();
293
```
294
 
295
Sometimes you may want to group your context items by a key that you provide to have an easier visual differentiation
296
when you look at your custom context items.
297
 
298
The Flare client allows you to also provide your own custom context groups like this:
299
 
300
```php
301
use Spatie\FlareClient\Flare;
302
 
303
\Spatie\Ignition\Ignition::make()
304
    ->runningInProductionEnvironment($boolean)
305
    ->sendToFlare($yourApiKey)
306
    ->configureFlare(function(Flare  $flare) {
307
        $flare->group('Custom information', [
308
            'key' => 'value',
309
            'another key' => 'another value',
310
        ]);
311
    })
312
    ->register();
313
```
314
 
315
### Anonymize request to Flare
316
 
317
By default, the Ignition collects information about the IP address of your application users. If you don't want to send this information to Flare, call `anonymizeIp()`.
318
 
319
```php
320
use Spatie\FlareClient\Flare;
321
 
322
\Spatie\Ignition\Ignition::make()
323
    ->runningInProductionEnvironment($boolean)
324
    ->sendToFlare($yourApiKey)
325
    ->configureFlare(function(Flare  $flare) {
326
        $flare->anonymizeIp();
327
    })
328
    ->register();
329
```
330
 
331
### Censoring request body fields
332
 
333
When an exception occurs in a web request, the Flare client will pass on any request fields that are present in the body.
334
 
335
In some cases, such as a login page, these request fields may contain a password that you don't want to send to Flare.
336
 
337
To censor out values of certain fields, you can use `censorRequestBodyFields`. You should pass it the names of the fields you wish to censor.
338
 
339
```php
340
use Spatie\FlareClient\Flare;
341
 
342
\Spatie\Ignition\Ignition::make()
343
    ->runningInProductionEnvironment($boolean)
344
    ->sendToFlare($yourApiKey)
345
    ->configureFlare(function(Flare  $flare) {
346
        $flare->censorRequestBodyFields(['password']);
347
    })
348
    ->register();
349
```
350
 
351
This will replace the value of any sent fields named "password" with the value "<CENSORED>".
352
 
353
### Using middleware to modify data sent to Flare
354
 
355
Before Flare receives the data that was collected from your local exception, we give you the ability to call custom middleware methods. These methods retrieve the report that should be sent to Flare and allow you to add custom information to that report.
356
 
357
A valid middleware is any class that implements `FlareMiddleware`.
358
 
359
```php
360
use Spatie\FlareClient\Report;
361
 
362
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
363
 
364
class MyMiddleware implements FlareMiddleware
365
{
366
    public function handle(Report $report, Closure $next)
367
    {
368
        $report->message("{$report->getMessage()}, now modified");
369
 
370
        return $next($report);
371
    }
372
}
373
```
374
 
375
```php
376
use Spatie\FlareClient\Flare;
377
 
378
\Spatie\Ignition\Ignition::make()
379
    ->runningInProductionEnvironment($boolean)
380
    ->sendToFlare($yourApiKey)
381
    ->configureFlare(function(Flare  $flare) {
382
        $flare->registerMiddleware([
383
            MyMiddleware::class,
384
        ])
385
    })
386
    ->register();
387
```
388
 
389
### Changelog
390
 
391
Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.
392
 
393
## Contributing
394
 
395
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
396
 
397
## Dev setup
398
 
399
Here are the steps you'll need to perform if you want to work on the UI of Ignition.
400
 
401
- clone (or move) `spatie/ignition`, `spatie/ignition-ui`, `spatie/laravel-ignition`, `spatie/flare-client-php` and `spatie/ignition-test` into the same directory (e.g. `~/code/flare`)
402
- create a new `package.json` file in `~/code/flare` directory:
403
```json
404
{
405
    "private": true,
406
    "workspaces": [
407
        "ignition-ui",
408
        "ignition"
409
    ]
410
}
411
```
412
- run `yarn install` in the `~/code/flare` directory
413
- in the `~/code/flare/ignition-test` directory
414
    - run `composer update`
415
    - run `cp .env.example .env`
416
    - run `php artisan key:generate`
417
- http://ignition-test.test/ should now work (= show the new UI). If you use valet, you might want to run `valet park` inside the `~/code/flare` directory.
418
    - http://ignition-test.test/ has a bit of everything
419
    - http://ignition-test.test/sql-error has a solution and SQL exception
420
 
421
## Security Vulnerabilities
422
 
423
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
424
 
425
## Credits
426
 
427
- [Spatie](https://spatie.be)
428
- [All Contributors](../../contributors)
429
 
430
## License
431
 
432
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.