laravel-debugbar/readme.md

220 lines
8.7 KiB
Markdown
Raw Permalink Normal View History

2023-02-01 17:22:49 +01:00
## Debugbar for Laravel
2020-08-20 12:28:00 +02:00
![Unit Tests](https://github.com/barryvdh/laravel-debugbar/workflows/Unit%20Tests/badge.svg)
2014-10-16 09:20:01 +02:00
[![Packagist License](https://poser.pugx.org/barryvdh/laravel-debugbar/license.png)](http://choosealicense.com/licenses/mit/)
2014-09-05 11:27:06 +02:00
[![Latest Stable Version](https://poser.pugx.org/barryvdh/laravel-debugbar/version.png)](https://packagist.org/packages/barryvdh/laravel-debugbar)
2014-09-05 11:25:42 +02:00
[![Total Downloads](https://poser.pugx.org/barryvdh/laravel-debugbar/d/total.png)](https://packagist.org/packages/barryvdh/laravel-debugbar)
2022-01-30 13:42:06 +01:00
[![Fruitcake](https://img.shields.io/badge/Powered%20By-Fruitcake-b2bc35.svg)](https://fruitcake.nl/)
2013-09-05 12:30:43 +02:00
2020-04-16 09:47:31 +02:00
This is a package to integrate [PHP Debug Bar](http://phpdebugbar.com/) with Laravel.
2013-09-05 17:10:19 +02:00
It includes a ServiceProvider to register the debugbar and attach it to the output. You can publish assets and configure it through Laravel.
It bootstraps some Collectors to work with Laravel and implements a couple custom DataCollectors, specific for Laravel.
2014-02-05 09:26:29 +01:00
It is configured to display Redirects and (jQuery) Ajax Requests. (Shown in a dropdown)
Read [the documentation](http://phpdebugbar.com/docs/) for more configuration options.
2024-03-11 13:37:20 +01:00
![Debugbar Dark Mode screenshot](https://github.com/barryvdh/laravel-debugbar/assets/973269/6600837a-8b2d-4acb-ab0c-158c9ca5439c)
2020-04-16 09:47:31 +02:00
2013-09-17 10:51:44 +02:00
### Note: Use the DebugBar only in development. Do not use Debugbar on publicly accessible websites, as it will leak information from stored requests (by design). It can also slow the application down (because it has to gather data). So when experiencing slowness, try disabling some of the collectors.
This package includes some custom collectors:
2014-02-18 10:06:53 +01:00
- QueryCollector: Show all queries, including binding + timing
2013-10-29 13:47:25 +01:00
- RouteCollector: Show information about the current Route.
- ViewCollector: Show the currently loaded views. (Optionally: display the shared data)
2013-09-18 14:37:47 +02:00
- EventsCollector: Show all events
2013-09-06 15:35:31 +02:00
- LaravelCollector: Show the Laravel version and Environment. (disabled by default)
2013-09-06 14:48:09 +02:00
- SymfonyRequestCollector: replaces the RequestCollector with more information about the request/response
2013-11-18 13:32:33 +01:00
- LogsCollector: Show the latest log entries from the storage logs. (disabled by default)
- FilesCollector: Show the files that are included/required by PHP. (disabled by default)
- ConfigCollector: Display the values from the config files. (disabled by default)
2017-07-25 10:23:19 +02:00
- CacheCollector: Display all cache events. (disabled by default)
2013-09-18 14:37:47 +02:00
Bootstraps the following collectors for Laravel:
- LogCollector: Show all Log messages
2023-09-19 22:22:07 +02:00
- SymfonyMailCollector for Mail
2013-09-18 14:37:47 +02:00
And the default collectors:
- PhpInfoCollector
- MessagesCollector
2013-10-10 11:02:23 +02:00
- TimeDataCollector (With Booting and Application timing)
2013-09-18 14:37:47 +02:00
- MemoryCollector
- ExceptionsCollector
It also provides a facade interface (`Debugbar`) for easy logging Messages, Exceptions and Time
2013-09-05 16:56:30 +02:00
2013-09-05 15:08:15 +02:00
## Installation
2017-08-28 22:21:05 +02:00
Require this package with composer. It is recommended to only require the package for development.
2016-07-29 16:57:14 +02:00
```shell
2017-08-28 22:21:05 +02:00
composer require barryvdh/laravel-debugbar --dev
```
2020-04-16 09:47:31 +02:00
Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
2017-07-21 14:13:24 +02:00
The Debugbar will be enabled when `APP_DEBUG` is `true`.
2017-06-01 19:48:56 +02:00
2015-01-26 20:45:48 +01:00
> If you use a catch-all/fallback route, make sure you load the Debugbar ServiceProvider before your own App ServiceProviders.
2020-04-16 09:47:31 +02:00
### Laravel without auto-discovery:
2017-07-21 14:13:24 +02:00
2024-05-02 09:29:50 +02:00
If you don't use auto-discovery, add the ServiceProvider to the providers list. For Laravel 11 or newer, add the ServiceProvider in bootstrap/providers.php. For Laravel 10 or older, add the ServiceProvider in config/app.php.
2015-07-09 16:42:18 +02:00
2016-07-29 16:57:14 +02:00
```php
Barryvdh\Debugbar\ServiceProvider::class,
```
2014-09-05 09:09:33 +02:00
If you want to use the facade to log messages, add this to your facades in app.php:
2016-07-29 16:57:14 +02:00
```php
'Debugbar' => Barryvdh\Debugbar\Facades\Debugbar::class,
```
2013-09-05 15:08:15 +02:00
2017-07-21 14:13:24 +02:00
The profiler is enabled by default, if you have APP_DEBUG=true. You can override that in the config (`debugbar.enabled`) or by setting `DEBUGBAR_ENABLED` in your `.env`. See more options in `config/debugbar.php`
You can also set in your config if you want to include/exclude the vendor files also (FontAwesome, Highlight.js and jQuery). If you already use them in your site, set it to false.
2016-06-10 23:51:24 +02:00
You can also only display the js or css vendors, by setting it to 'js' or 'css'. (Highlight.js requires both css + js, so set to `true` for syntax highlighting)
2013-09-05 15:08:15 +02:00
#### Copy the package config to your local config with the publish command:
2015-01-26 20:45:48 +01:00
2016-07-29 16:57:14 +02:00
```shell
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
```
2013-09-05 15:08:15 +02:00
### Laravel with Octane:
Make sure to add LaravelDebugbar to your flush list in `config/octane.php`.
```php
'flush' => [
\Barryvdh\Debugbar\LaravelDebugbar::class,
],
```
2015-07-09 16:42:18 +02:00
### Lumen:
For Lumen, register a different Provider in `bootstrap/app.php`:
2016-07-29 16:57:14 +02:00
```php
2015-07-13 14:36:23 +02:00
if (env('APP_DEBUG')) {
$app->register(Barryvdh\Debugbar\LumenServiceProvider::class);
}
2015-07-09 16:42:18 +02:00
```
To change the configuration, copy the file to your config folder and enable it:
2016-07-29 16:57:14 +02:00
```php
2015-07-09 16:42:18 +02:00
$app->configure('debugbar');
```
## Usage
2013-09-05 16:07:42 +02:00
2014-09-05 09:09:33 +02:00
You can now add messages using the Facade (when added), using the PSR-3 levels (debug, info, notice, warning, error, critical, alert, emergency):
2013-09-05 16:07:42 +02:00
```php
Debugbar::info($object);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');
```
2013-09-05 16:17:05 +02:00
And start/stop timing:
```php
Debugbar::startMeasure('render','Time for rendering');
Debugbar::stopMeasure('render');
Debugbar::addMeasure('now', LARAVEL_START, microtime(true));
Debugbar::measure('My long operation', function() {
// Do something…
});
```
2013-09-05 16:35:05 +02:00
Or log exceptions:
```php
try {
throw new Exception('foobar');
} catch (Exception $e) {
Debugbar::addThrowable($e);
}
```
2014-09-05 09:09:33 +02:00
There are also helper functions available for the most common calls:
```php
// All arguments will be dumped as a debug message
debug($var1, $someString, $intValue, $object);
// `$collection->debug()` will return the collection and dump it as a debug message. Like `$collection->dump()`
collect([$var1, $someString])->debug();
start_measure('render','Time for rendering');
stop_measure('render');
add_measure('now', LARAVEL_START, microtime(true));
measure('My long operation', function() {
// Do something…
});
```
2013-09-10 13:24:55 +02:00
If you want you can add your own DataCollectors, through the Container or the Facade:
```php
Debugbar::addCollector(new DebugBar\DataCollector\MessagesCollector('my_messages'));
//Or via the App container:
$debugbar = App::make('debugbar');
$debugbar->addCollector(new DebugBar\DataCollector\MessagesCollector('my_messages'));
```
2013-10-30 12:35:18 +01:00
2013-10-30 12:38:12 +01:00
By default, the Debugbar is injected just before `</body>`. If you want to inject the Debugbar yourself,
2013-10-30 12:35:18 +01:00
set the config option 'inject' to false and use the renderer yourself and follow http://phpdebugbar.com/docs/rendering.html
```php
$renderer = Debugbar::getJavascriptRenderer();
```
2013-10-30 12:35:18 +01:00
Note: Not using the auto-inject, will disable the Request information, because that is added After the response.
2013-11-17 18:04:52 +01:00
You can add the default_request datacollector in the config as alternative.
## Enabling/Disabling on run time
You can enable or disable the debugbar during run time.
```php
\Debugbar::enable();
\Debugbar::disable();
```
2013-11-17 18:04:52 +01:00
2013-12-21 16:07:06 +01:00
NB. Once enabled, the collectors are added (and could produce extra overhead), so if you want to use the debugbar in production, disable in the config and only enable when needed.
2014-08-09 22:37:14 +02:00
## Storage
Debugbar remembers previous requests, which you can view using the Browse button on the right. This will only work if you enable `debugbar.storage.open` in the config.
Make sure you only do this on local development, because otherwise other people will be able to view previous requests.
In general, Debugbar should only be used locally or at least restricted by IP.
It's possible to pass a callback, which will receive the Request object, so you can determine access to the OpenHandler storage.
2014-08-09 22:37:14 +02:00
## Twig Integration
Laravel Debugbar comes with two Twig Extensions. These are tested with [rcrowe/TwigBridge](https://github.com/rcrowe/TwigBridge) 0.6.x
2014-10-31 16:49:27 +01:00
Add the following extensions to your TwigBridge config/extensions.php (or register the extensions manually)
2014-08-09 22:37:14 +02:00
```php
'Barryvdh\Debugbar\Twig\Extension\Debug',
'Barryvdh\Debugbar\Twig\Extension\Dump',
'Barryvdh\Debugbar\Twig\Extension\Stopwatch',
```
2014-08-09 22:37:14 +02:00
The Dump extension will replace the [dump function](http://twig.sensiolabs.org/doc/functions/dump.html) to output variables using the DataFormatter. The Debug extension adds a `debug()` function which passes variables to the Message Collector,
2014-08-09 22:37:14 +02:00
instead of showing it directly in the template. It dumps the arguments, or when empty; all context variables.
2016-07-29 16:57:14 +02:00
```twig
{{ debug() }}
{{ debug(user, categories) }}
```
2014-08-09 22:37:14 +02:00
The Stopwatch extension adds a [stopwatch tag](http://symfony.com/blog/new-in-symfony-2-4-a-stopwatch-tag-for-twig) similar to the one in Symfony/Silex Twigbridge.
2016-07-29 16:57:14 +02:00
```twig
{% stopwatch "foo" %}
…some things that gets timed
{% endstopwatch %}
```