Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | 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
 
109
Out of the box, Ignition will display solutions for common errors such as bad methods calls, or using undefined
110
properties.
111
 
112
#### Adding a solution directly to an exception
113
 
114
To add a solution text to your exception, let the exception implement the `Spatie\Ignition\Contracts\ProvidesSolution`
115
interface.
116
 
117
This interface requires you to implement one method, which is going to return the `Solution` that users will see when
118
the exception gets thrown.
119
 
120
```php
121
use Spatie\Ignition\Contracts\Solution;
122
use Spatie\Ignition\Contracts\ProvidesSolution;
123
 
124
class CustomException extends Exception implements ProvidesSolution
125
{
126
    public function getSolution(): Solution
127
    {
128
        return new CustomSolution();
129
    }
130
}
131
```
132
 
133
```php
134
use Spatie\Ignition\Contracts\Solution;
135
 
136
class CustomSolution implements Solution
137
{
138
    public function getSolutionTitle(): string
139
    {
140
        return 'The solution title goes here';
141
    }
142
 
143
    public function getSolutionDescription(): string
144
    {
145
        return 'This is a longer description of the solution that you want to show.';
146
    }
147
 
148
    public function getDocumentationLinks(): array
149
    {
150
        return [
151
            'Your documentation' => 'https://your-project.com/relevant-docs-page',
152
        ];
153
    }
154
}
155
```
156
 
157
This is how the exception would be displayed if you were to throw it.
158
 
159
![Screenshot of solution](https://spatie.github.io/ignition/solution.png)
160
 
161
#### Using solution providers
162
 
163
Instead of adding solutions to exceptions directly, you can also create a solution provider. While exceptions that
164
return a solution, provide the solution directly to Ignition, a solution provider allows you to figure out if an
165
exception can be solved.
166
 
167
For example, you could create a custom "Stack Overflow solution provider", that will look up if a solution can be found
168
for a given throwable.
169
 
170
Solution providers can be added by third party packages or within your own application.
171
 
172
A solution provider is any class that implements the \Spatie\Ignition\Contracts\HasSolutionsForThrowable interface.
173
 
174
This is how the interface looks like:
175
 
176
```php
177
interface HasSolutionsForThrowable
178
{
179
    public function canSolve(Throwable $throwable): bool;
180
 
181
    /** @return \Spatie\Ignition\Contracts\Solution[] */
182
    public function getSolutions(Throwable $throwable): array;
183
}
184
```
185
 
186
When an error occurs in your app, the class will receive the `Throwable` in the `canSolve` method. In that method you
187
can decide if your solution provider is applicable to the `Throwable` passed. If you return `true`, `getSolutions` will
188
get called.
189
 
190
To register a solution provider to Ignition you must call the `addSolutionProviders` method.
191
 
192
```php
193
\Spatie\Ignition\Ignition::make()
194
    ->addSolutionProviders([
195
        YourSolutionProvider::class,
196
        AnotherSolutionProvider::class,
197
    ])
198
    ->register();
199
```
200
 
201
### Sending exceptions to Flare
202
 
203
Ignition comes with the ability to send exceptions to [Flare](https://flareapp.io), an exception monitoring service. Flare
204
can notify you when new exceptions are occurring in your production environment.
205
 
206
To send exceptions to Flare, simply call the `sendToFlareMethod` and pass it the API key you got when creating a project
207
on Flare.
208
 
209
You probably want to combine this with calling `runningInProductionEnvironment`. That method will, when passed a truthy
210
value, not display the Ignition error page, but only send the exception to Flare.
211
 
212
```php
213
\Spatie\Ignition\Ignition::make()
214
    ->runningInProductionEnvironment($boolean)
215
    ->sendToFlare($yourApiKey)
216
    ->register();
217
```
218
 
219
When you pass a falsy value to `runningInProductionEnvironment`, the Ignition error page will get shown, but no
220
exceptions will be sent to Flare.
221
 
222
### Sending custom context to Flare
223
 
224
When you send an error to Flare, you can add custom information that will be sent along with every exception that
225
happens in your application. This can be very useful if you want to provide key-value related information that
226
furthermore helps you to debug a possible exception.
227
 
228
```php
229
use Spatie\FlareClient\Flare;
230
 
231
\Spatie\Ignition\Ignition::make()
232
    ->runningInProductionEnvironment($boolean)
233
    ->sendToFlare($yourApiKey)
234
    ->configureFlare(function(Flare  $flare) {
235
        $flare->context('Tenant', 'My-Tenant-Identifier');
236
    })
237
    ->register();
238
```
239
 
240
Sometimes you may want to group your context items by a key that you provide to have an easier visual differentiation
241
when you look at your custom context items.
242
 
243
The Flare client allows you to also provide your own custom context groups like this:
244
 
245
```php
246
use Spatie\FlareClient\Flare;
247
 
248
\Spatie\Ignition\Ignition::make()
249
    ->runningInProductionEnvironment($boolean)
250
    ->sendToFlare($yourApiKey)
251
    ->configureFlare(function(Flare  $flare) {
252
        $flare->group('Custom information', [
253
            'key' => 'value',
254
            'another key' => 'another value',
255
        ]);
256
    })
257
    ->register();
258
```
259
 
260
### Anonymize request to Flare
261
 
262
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()`.
263
 
264
```php
265
use Spatie\FlareClient\Flare;
266
 
267
\Spatie\Ignition\Ignition::make()
268
    ->runningInProductionEnvironment($boolean)
269
    ->sendToFlare($yourApiKey)
270
    ->configureFlare(function(Flare  $flare) {
271
        $flare->anonymizeIp();
272
    })
273
    ->register();
274
```
275
 
276
### Censoring request body fields
277
 
278
When an exception occurs in a web request, the Flare client will pass on any request fields that are present in the body.
279
 
280
In some cases, such as a login page, these request fields may contain a password that you don't want to send to Flare.
281
 
282
To censor out values of certain fields, you can use `censorRequestBodyFields`. You should pass it the names of the fields you wish to censor.
283
 
284
```php
285
use Spatie\FlareClient\Flare;
286
 
287
\Spatie\Ignition\Ignition::make()
288
    ->runningInProductionEnvironment($boolean)
289
    ->sendToFlare($yourApiKey)
290
    ->configureFlare(function(Flare  $flare) {
291
        $flare->censorRequestBodyFields(['password']);
292
    })
293
    ->register();
294
```
295
 
296
This will replace the value of any sent fields named "password" with the value "<CENSORED>".
297
 
298
### Using middleware to modify data sent to Flare
299
 
300
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.
301
 
302
A valid middleware is any class that implements `FlareMiddleware`.
303
 
304
```php
305
use Spatie\FlareClient\Report;
306
 
307
use Spatie\FlareClient\FlareMiddleware\FlareMiddleware;
308
 
309
class MyMiddleware implements FlareMiddleware
310
{
311
    public function handle(Report $report, Closure $next)
312
    {
313
        $report->message("{$report->getMessage()}, now modified");
314
 
315
        return $next($report);
316
    }
317
}
318
```
319
 
320
```php
321
use Spatie\FlareClient\Flare;
322
 
323
\Spatie\Ignition\Ignition::make()
324
    ->runningInProductionEnvironment($boolean)
325
    ->sendToFlare($yourApiKey)
326
    ->configureFlare(function(Flare  $flare) {
327
        $flare->registerMiddleware([
328
            MyMiddleware::class,
329
        ])
330
    })
331
    ->register();
332
```
333
 
334
### Changelog
335
 
336
Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently.
337
 
338
## Contributing
339
 
340
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
341
 
342
## Dev setup
343
 
344
Here are the steps you'll need to perform if you want to work on the UI of Ignition.
345
 
346
- 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`)
347
- create a new `package.json` file in `~/code/flare` directory:
348
```json
349
{
350
    "private": true,
351
    "workspaces": [
352
        "ignition-ui",
353
        "ignition"
354
    ]
355
}
356
```
357
- run `yarn install` in the `~/code/flare` directory
358
- in the `~/code/flare/ignition-test` directory
359
    - run `composer update`
360
    - run `cp .env.example .env`
361
    - run `php artisan key:generate`
362
- 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.
363
    - http://ignition-test.test/ has a bit of everything
364
    - http://ignition-test.test/sql-error has a solution and SQL exception
365
 
366
## Security Vulnerabilities
367
 
368
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
369
 
370
## Credits
371
 
372
- [Spatie](https://spatie.be)
373
- [All Contributors](../../contributors)
374
 
375
## License
376
 
377
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.