laravel-debugbar README

The project's README from github.com/fruitcake/laravel-debugbar. As one markdown file: README.md.

Debugbar for Laravel

Unit Tests Packagist License Latest Stable Version Total Downloads Fruitcake

This is a package to integrate PHP Debug Bar with Laravel. 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. It is configured to display Redirects and Ajax/Livewire Requests, which are shown in a dropdown. Read the documentation for more configuration options.

Debugbar Dark Mode screenshot

[!CAUTION] Use the DebugBar only in development. Do not use Debugbar on publicly accessible websites, as it will leak information from stored requests (by design).

[!WARNING] It can also slow the application down (because it has to gather and render data). So when experiencing slowness, try disabling some of the collectors.

This package includes some custom collectors:

Bootstraps the following collectors for Laravel:

And the default collectors:

It also provides a facade interface (Debugbar) for easy logging Messages, Exceptions and Time

Installation

Require this package with composer. It is recommended to only require the package for development.

composer require fruitcake/laravel-debugbar --dev

Note: The package name has changed to fruitcake/laravel-debugbar. If you're using barryvdh/laravel-debugbar, you can safely replace this with the new package name: composer remove barryvdh/laravel-debugbar --dev --no-scripts

Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

The Debugbar will be enabled when APP_DEBUG is true and when the environment is not production or testing.

You can disable it 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. 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)

Copy the package config to your local config with the publish command:

php artisan vendor:publish --provider='Fruitcake\LaravelDebugbar\ServiceProvider'

Laravel with Octane:

Laravel Debugbar 4.x works out of the box with Octane. No need to add anything to your config.

If you're upgrading from Laravel Debugbar 3.x, remove the 'flush' config for Debugbar in config/octane.php.

Usage

You can now add messages using the Facade (when added), using the PSR-3 levels (debug, info, notice, warning, error, critical, alert, emergency):

Debugbar::info($object);
Debugbar::error('Error!');
Debugbar::warning('Watch out…');
Debugbar::addMessage('Another message', 'mylabel');

And start/stop timing:

Debugbar::startMeasure('render','Time for rendering');
Debugbar::stopMeasure('render');
Debugbar::addMeasure('now', LARAVEL_START, microtime(true));
Debugbar::measure('My long operation', function() {
    // Do something…
});

Or log exceptions:

try {
    throw new Exception('foobar');
} catch (Exception $e) {
    Debugbar::addThrowable($e);
}

There are also helper functions available for the most common calls:

// 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();

debugbar()->startMeasure('render','Time for rendering');
debugbar()->stopMeasure('render');
debugbar()->addMeasure('now', LARAVEL_START, microtime(true));
debugbar()->measure('My long operation', function() {
    // Do something…
});

If you want you can add your own DataCollectors, through the Container or the Facade:

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'));

By default, the Debugbar is injected just before </body>. If you want to inject the Debugbar yourself, set the config option 'inject' to false and use the renderer yourself and follow https://php-debugbar.com/docs/rendering

$renderer = Debugbar::getJavascriptRenderer();

Note: Not using the auto-inject, will disable the Request information, because that is added After the response. 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.

\Debugbar::enable();
\Debugbar::disable();

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.

Note: by default Debugbar can only be enabled in debug mode and non-production environments. It's highly recommended to don't install this in production at all. If you really need to enable it in production, you can set debugbar.force_allow_enable to true, or set DEBUGBAR_FORCE_ALLOW_ENABLE=true in your .env. This will not enable the Debugbar, but will run the ServiceProvider bootstrap so you can enable it after booting.

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.

Twig Integration

Laravel Debugbar comes with two Twig Extensions. These are tested with rcrowe/TwigBridge 0.6.x

Add the following extensions to your TwigBridge config/extensions.php (or register the extensions manually)

'Fruitcake\LaravelDebugbar\Twig\Extension\Debug',
'Fruitcake\LaravelDebugbar\Twig\Extension\Dump',
'Fruitcake\LaravelDebugbar\Twig\Extension\Stopwatch',

The Dump extension will replace the dump function to output variables using the DataFormatter. The Debug extension adds a debug() function which passes variables to the Message Collector, instead of showing it directly in the template. It dumps the arguments, or when empty; all context variables.

{{ debug() }}
{{ debug(user, categories) }}

The Stopwatch extension adds a stopwatch tag similar to the one in Symfony/Silex Twigbridge.

{% stopwatch "foo" %}
    …some things that gets timed
{% endstopwatch %}

laravel-debugbar (fruitcake/laravel-debugbar)

Searching Database…

这是什么东西?一个把 PHP Debug Bar 集成到 Laravel 应用里的扩展包:通过一个 ServiceProvider 注册调试栏,并在你的页面中注入一个调试工具条,显示查询、日志、耗时等信息。
这是单独运行的工具,还是加到我的 Laravel 项目里的东西?是加到你项目里的:它以 Composer 包的形式随 Laravel 启动,内置了针对 Laravel 的收集器,还有专门为它编写的自定义 DataCollector。
调试栏显示出来之前,我需要配置什么吗?不需要:只要 APP_DEBUG 为 true,并且环境不是 production 或 testing,它就会自动启用。你也可以通过 debugbar.enabled 配置项或 .env 里的 DEBUGBAR_ENABLED 变量来开关它。
我可以在生产环境一直开着它吗?不推荐:它本身就会泄露已存储请求中的信息,并且在收集数据时会拖慢应用。README 明确说不要在生产环境安装它;虽然有个 force_allow_enable 开关,但它只是引导启动 provider,让你能在运行时手动开启。
它记录的请求都存到哪里去了?之前的请求会保存在本地,可以通过 Browse 按钮重新查看,但前提是你开启了 debugbar.storage.open。README 警告说这个功能只适合本地开发使用,否则别人可能会看到你过去的请求。
工具条里到底能看到什么?带绑定参数和耗时的数据库查询、当前路由、加载的视图、事件、缓存事件、日志条目、发送的邮件、异常、内存占用和应用耗时——还有 Laravel 版本和环境信息。重定向以及 Ajax/Livewire 请求会显示在一个下拉菜单里。
我能记录自己的消息,或者测量某段代码花了多久吗?可以:Debugbar facade 支持按 PSR-3 级别记录消息,可以开始和停止计时、记录异常,常用调用还有辅助函数。你也可以通过容器或 facade 注册自己的 DataCollector。
它和 Laravel Octane 兼容吗?兼容,Laravel Debugbar 4.x 开箱即用地支持 Octane。从 3.x 升级时,你只需要删掉 config/octane.php 里 debugbar 的 'flush' 配置项。
为什么它现在叫 fruitcake/laravel-debugbar?这个包从 barryvdh/laravel-debugbar 改名为 fruitcake/laravel-debugbar。README 说老用户可以放心切换:先用 composer remove --dev 移除旧包,再安装新名字的包即可。
Creator
fruitcake
Size
15.4 MB
Stars
19.3K
Age
13y
Commits · 90 Days