Laravel Debugbar 翻译

Laravel Debugbar

Packagist License
Latest Stable Version
Total Downloads

注意在v3版本中: Debugbar将会在引入后会自动被启用,但是仍然需要将APP_DEBUG设置为true!

Laravel<5.5的版本请始用2.4 版本!

这是一个将 PHP Debug Bar 整合进Laravel5的扩展包.
它包括一个ServiceProvider,用于注册debugbar并将其附加到输出。您可以通过Laravel发布资产并对其进行配置。
它将在Laravel运行的时候创建一些Collectors并且实现两份用户的DataCollectors对象。
默认配置文件它将显示重定向和Ajax请求。(显示在下拉菜单中)
更多配置选项请查阅 配置文档

截图

注意: DebugBar仅供开发环境下始用. 它将导致应用程序运行缓慢 (因为它需要手机数据). 所以在运行缓慢的时候,尝试禁用一些收集器.

该扩展包包含以下自定义收集器:
- ueryCollector:显示所有查询,包含绑定和时间。
- outeCollector:显示当前路由信息。
- iewCollector:显示当前加载的视图。(可选:显示共享数据)
- ventsCollector:显示所有事件。
- aravelCollector:显示Laravel版本和环境。(默认关闭)
- ymfonyRequestCollector:替换 RequestCollector,显示关于请求/响应的更多信息。
- ogsCollector:显示来自日志文件的最新日志条目。(默认关闭)
- ilesCollector:显示 PHP include/require 的文件。(默认关闭)
- ConfigCollector:显示配置文件配置值。(默认关闭)
- CacheCollector: 显示缓存事件。(默认关闭)

Laravel启动时包含的收集器:
- LogCollector: 显示所有日志信息
- 为邮件提供的 SwiftMailCollector 和 SwiftLogCollectorl

以及以下默认收集器:
- PhpInfoCollector
- MessageCollector
- TimeDataCollector(包含启动及应用时间)
- MemoryCollector
- ExceptionsCollector

此外还提供了一个门面用于记录消息、时间和异常。

安装

使用Composerrequire --dev 命令导入这个包。这将仅在开发模式下引入这个包。

composer require barryvdh/laravel-debugbar --dev

在Laravel 5.5 中将始用 Package Auto-Discovery自动导入这个包, 所以你不需要手动添加 ServiceProvider.

Debugbar 将会在 APP_DEBUGtrue 的时候启用.

If you use a catch-all/fallback route, make sure you load the Debugbar ServiceProvider before your own App ServiceProviders.

Laravel 5.5+:

如果你没有始用 auto-discovery ,需要添加 ServiceProviderconfig/app.phpproviders 数组中:

Barryvdh\Debugbar\ServiceProvider::class,

如果你想要始用Facade来记录信息,添加这个到 app.phpfacedes:

'Debugbar' => Barryvdh\Debugbar\Facade::class,

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.
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="Barryvdh\Debugbar\ServiceProvider"

Lumen:

For Lumen, register a different Provider in bootstrap/app.php:

if (env('APP_DEBUG')) {
 $app->register(Barryvdh\Debugbar\LumenServiceProvider::class);
}

To change the configuration, copy the file to your config folder and enable it:

$app->configure('debugbar');

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

start_measure('render','Time for rendering');
stop_measure('render');
add_measure('now', LARAVEL_START, microtime(true));
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 http://phpdebugbar.com/docs/rendering.html

$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.

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)

'Barryvdh\Debugbar\Twig\Extension\Debug',
'Barryvdh\Debugbar\Twig\Extension\Dump',
'Barryvdh\Debugbar\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 %}
点赞