DebugBar provides debugging and profiling information during development.
Enable DebugBar via config/app.php:
// config/app.php
return [
// ...
'debugbar' => env('DEBUGBAR_ENABLED', false),
];Or in your .env file:
DEBUGBAR_ENABLED=trueGet the DebugBar instance using the helper function:
$bar = debugger();
if ($bar !== null) {
// DebugBar is enabled
}Log messages at different levels:
$bar = debugger();
if ($bar !== null) {
$bar->log('info', 'User logged in', ['user_id' => 123]);
$bar->log('warning', 'Slow query detected');
$bar->log('error', 'Something went wrong');
}Supported log levels: debug, info, notice, warning, error, critical, alert, emergency
Inspect variables in the DebugBar:
$bar = debugger();
if ($bar !== null) {
$bar->addDump($variable);
$bar->addDump($user, 'Current User');
$bar->addDump($array, 'Request Data', __FILE__, __LINE__);
}Mark points in your code to measure execution time:
$bar = debugger();
if ($bar !== null) {
$bar->mark('Start of process');
}
// ... your code ...
if ($bar !== null) {
$bar->mark('End of process');
}Record database queries with timing information:
$bar = debugger();
if ($bar !== null) {
$bar->addQuery(
sql: "SELECT * FROM users WHERE id = ?",
params: [1],
durationMs: 0.523,
connection: 'mysql'
);
}Track exceptions:
$bar = debugger();
try {
// code that throws
} catch (\Exception $e) {
if ($bar !== null) {
$bar->addException($e);
}
}Register exception handlers to automatically capture all unhandled exceptions:
$bar = debugger();
if ($bar !== null) {
$bar->registerExceptionHandlers();
}use Marwa\Framework\Controllers\Controller;
use Psr\Http\Message\ResponseInterface;
class UserController extends Controller
{
public function index(): ResponseInterface
{
$bar = debugger();
if ($bar !== null) {
$bar->mark('Controller started');
$bar->log('info', 'Loading users');
}
$users = User::all();
if ($bar !== null) {
$bar->addDump($users->toArray(), 'Users');
$bar->mark('Controller finished');
}
return view('users.index', ['users' => $users]);
}
}The DebugBar automatically injects itself into HTML responses before the closing </body> tag when:
- DebugBar is enabled in config
- The response Content-Type is
text/html - The response body contains a
</body>tag
- DebugBar should be disabled in production
- The
debugger()helper returnsnullwhen DebugBar is disabled (safe to use without conditionals in production) - Maximum 100 dumps are stored to prevent memory issues