yiisoft/var-dumper
Yii VarDumper enhances var_dump()/var_export() with safe handling of recursive references, syntax highlighting, and closure export. Includes handy d(), dump(), and dd() helpers for quick debugging. Configurable depth and highlighting.
Installation Update Composer to target PHP 8.0–8.5 (required for this release):
composer require yiisoft/var-dumper:^1.7.1
No additional configuration is required—it remains a standalone utility.
First Use Case Dump a variable in a Laravel controller or Blade template (PHP 8.0–8.5 only):
use Yiisoft\VarDumper\VarDumper;
// In a controller
public function show(Request $request) {
$data = $request->all();
VarDumper::dump($data);
// Outputs improved HTML syntax-highlighted dumps in browsers and colored CLI output.
}
Where to Look First
dd() helpers, but ensure your app uses PHP 8.0–8.5.Replacing dd() or dump() (PHP 8.0+)
Replace Laravel’s dd() (halts execution) or dump() (console-only) with VarDumper for consistent, syntax-highlighted output:
// Instead of:
dd($user); // Halts execution (PHP 8.0+ compatible)
dump($user); // Console-only
// Use:
VarDumper::dump($user); // Works in CLI/browser, with improved HTML syntax highlighting.
Leveraging Enhanced HTML Output
$request->all():
<pre class="var-dump">
<span class="var-dump-key">"user"</span>: <span class="var-dump-value">"john"</span>,
<span class="var-dump-key">"roles"</span>: [
<span class="var-dump-value">"admin"</span>,
<span class="var-dump-value">"editor"</span>
]
</pre>
Customizing Output for PHP 8.0+
TypedArray, WeakMap) with custom handlers:
VarDumper::setHandler(new class implements VarDumperHandlerInterface {
public function handle($var) {
if ($var instanceof TypedArray) {
return "TypedArray(" . count($var) . ")";
}
return VarDumper::dumpAsString($var);
}
});
Logging Debug Data Log formatted dumps to Laravel’s log system (PHP 8.0+):
use Illuminate\Support\Facades\Log;
Log::debug(VarDumper::dumpAsString($this->request->headers));
Debugging API Requests (PHP 8.0+)
public function api(Request $request) {
VarDumper::dump($request->json()->all());
// Outputs syntax-highlighted JSON in browser/CLI.
}
Database Query Debugging
$users = User::where('active', true)->get();
VarDumper::dump($users->toArray());
// Collections render with improved HTML structure.
Event Listeners (PHP 8.0+)
public function handle(UserLoggedIn $event) {
VarDumper::dump($event->user->with('roles')->toArray());
// Nested relationships display with syntax highlighting.
}
barryvdh/laravel-debugbar for enhanced panels (ensure PHP 8.0+ compatibility).VarDumper handlers.public function testUserCreation() {
$user = User::create([...]);
VarDumper::dump($user->fresh());
// ...
}
PHP Version Constraint (BREAKING)
yiisoft/var-dumper:^1.6 if stuck on PHP <8.0.Performance Overhead
if (app()->environment('local')) {
VarDumper::dump($variable);
}
Circular References
setMaxItems() (PHP 8.0+):
VarDumper::setMaxItems(50); // Limit nested items.
HTML Output in APIs
dumpAsString():
return response()->json(['debug' => VarDumper::dumpAsString($data)]);
Disable Output Temporarily Override the handler to suppress output (PHP 8.0+):
VarDumper::setHandler(new class {
public function handle($var) { return null; }
});
Log Dumps to File
file_put_contents(
storage_path('logs/debug_dump.log'),
VarDumper::dumpAsString($variable)
);
Custom Exceptions Catch and log dumps for errors (PHP 8.0+):
try {
$result = riskyOperation();
} catch (\Exception $e) {
VarDumper::dump($context);
throw $e;
}
Custom Handlers for PHP 8.0+ Types
Register handlers for PHP 8.0+ features (e.g., Attribute, WeakReference):
VarDumper::setHandler(new class implements VarDumperHandlerInterface {
public function handle($var) {
if ($var instanceof Attribute) {
return "Attribute: " . $var->getName();
}
return VarDumper::dumpAsString($var);
}
});
Override Default Formatter Replace the formatter for global changes (PHP 8.0+):
VarDumper::setFormatter(new \Yiisoft\VarDumper\Formatter\HtmlFormatter());
Laravel Service Provider (PHP 8.0+)
Bind VarDumper to the container:
// In AppServiceProvider
$this->app->singleton(VarDumper::class, function () {
return new VarDumper();
});
Blade Directives (PHP 8.0+) Create a custom Blade directive:
// In AppServiceProvider
Blade::directive('dump', function ($expression) {
return "<?php echo Yiisoft\VarDumper\VarDumper::dump({$expression}); ?>";
});
Usage:
@dump($variable)
setMaxItems()).memory_limit. Use:
VarDumper::setMaxString(100); // Truncate strings >100 chars.
VarDumper::setMaxItems(50); // Limit nested items.
EchoHandler includes it.How can I help you explore Laravel packages today?