yiisoft/yii2-debug
Yii2 Debug adds a bottom toolbar and dedicated pages to inspect requests, logs, DB queries, profiling, and more during development. Install via Composer and enable the debug module in your app config to quickly diagnose issues.
Installation:
composer require --prefer-dist yiisoft/yii2-debug
Add to config/web.php (or your environment config):
'bootstrap' => ['debug'],
'modules' => [
'debug' => [
'class' => 'yii\debug\Module',
],
],
First Use Case:
YII_DEBUG = true in your index.php or index-test.php.Where to Look First:
/debug/default/view (shows detailed request data)./debug/default/log (view all logged messages)./debug/default/db (query logs, execution times).Debugging a Request:
/debug/default/view for a full request dump (headers, POST/GET data, cookies, etc.).Database Debugging:
db component is configured in components./debug/default/db with:
'panels' => [
'db' => [
'class' => 'yii\debug\panels\DbPanel',
'defaultOrder' => ['duration' => SORT_DESC],
],
],
Customizing Logs:
log component to include debug-level messages:
'components' => [
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning', 'trace'],
],
],
],
],
/debug/default/log.Profiling:
/debug/default/profile.IDE Integration:
traceLine for direct file/line jumps in your IDE:
'traceLine' => '<a href="phpstorm://open?url={file}&line={line}">{file}:{line}</a>',
tracePathMappings:
'tracePathMappings' => [
'/app' => '/absolute/path/to/your/app',
],
Environment-Specific Debugging:
'allowedIPs' => ['192.168.1.100', '127.0.0.1'],
if (YII_ENV_PROD) {
unset($config['bootstrap'][array_search('debug', $config['bootstrap'])]);
unset($config['modules']['debug']);
}
URL Manager:
If using enableStrictParsing, add this rule:
'urlManager' => [
'enableStrictParsing' => true,
'rules' => [
'debug/<controller>/<action>' => 'debug/<controller>/<action>',
],
],
Custom Panels:
Extend yii\debug\Panel to create reusable debug panels (e.g., for tracking rendered views, cache hits, or custom metrics).
Example:
namespace app\panels;
use yii\debug\Panel;
class CachePanel extends Panel {
public function getName() { return 'Cache'; }
public function getSummary() { /* ... */ }
public function getDetail() { /* ... */ }
public function save() { return $this->cacheStats; }
}
Register in config:
'panels' => [
'cache' => ['class' => 'app\panels\CachePanel'],
],
JavaScript Integration:
Listen for the yii.debug.toolbar_attached event to interact with the toolbar dynamically:
document.addEventListener('yii.debug.toolbar_attached', function(e) {
const toolbar = e.target;
toolbar.querySelector('.yii-debug-toolbar__block').addEventListener('click', (e) => {
e.preventDefault();
alert('Toolbar clicked!');
});
});
Performance Monitoring:
Error Debugging:
YII_DEBUG to control verbosity:
defined('YII_DEBUG') or define('YII_DEBUG', true); // Enable full debugging
Permission Issues:
@runtime/debug directory is writable by the web server user.chmod -R 775 runtime/debug (adjust permissions as needed).Strict Parsing Conflicts:
enableStrictParsing, forget the debug/ rule, and the toolbar/debug panels will 404.IP Restrictions:
allowedIPs in non-local environments will hide the toolbar.'allowedIPs' => ['your.ip.here', '127.0.0.1'],
IDE Links Not Working:
traceLine links don’t open in your IDE:
phpstorm:// for PhpStorm).tracePathMappings points to the correct host path.file:// link first:
'traceLine' => '<a href="file://{file}&line={line}">{file}:{line}</a>',
Performance Overhead:
if (YII_ENV_PROD) {
$config['bootstrap'] = array_filter($config['bootstrap'], fn($item) => $item !== 'debug');
}
Missing Panels:
db) is configured in components.Log Overload:
traceLevel too high (e.g., 3) in production can bloat logs.YII_DEBUG to toggle:
'traceLevel' => YII_DEBUG ? 3 : 0,
Toolbar Not Showing?:
YII_DEBUG is true in index.php.debug module is bootstrapped and configured.Debug Data Not Persisting:
@runtime/debug is writable.Custom Panel Not Loading:
app\panels\CachePanel).panels config:
'panels' => ['cache' => ['class' => 'app\panels\CachePanel']],
Slow Debug Panels:
/debug/default/view.Docker-Specific Issues:
traceLine are incorrect, use tracePathMappings:
'tracePathMappings' => [
'/var/www/html' => '/host/path/to/app',
],
How can I help you explore Laravel packages today?