Debug laravel application with php debugbar

Debugging is a helpful feature for developers to identify the causes of issues. Nowadays it’s easy to debug applications because most of the modern application frameworks provide you with inbuilt debug functionality. The Symfony Framework implements by default a useful debugging bar.

Laravel Debugbar 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 them through Laravel. It bootstraps some Collectors to work with Laravel and implements a couple of custom Data Collectors, specific to Laravel.

Install & Config The Laravel Debugbar Package

To install this package, enter the below command into your terminal :

composer require barryvdh/laravel-debugbar

Before using this package you need to enable debugging for your application. so open the .env file and make the below change :

APP_DEBUG=true

If you don’t use auto-discovery, add the ServiceProvider & Facade manually in config/app.php:

'providers' => [
    ....
    Barryvdh\Debugbar\ServiceProvider::class,
],
'aliases' => [
    ....
    'Debugbar' => Barryvdh\Debugbar\Facade::class,
]

After adding this package to the application configuration. you need to publish related files to your application. Open the terminal and enter the below command :

php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"

Note: If you are facing any error you might need clear configuration caches.

Using Laravel Debugbar

Now you can start debugging. you can use static methods to display messages for debug, info, notice, warning, error, critical, alert, and emergency. Let’s add messages to the home page to check our setup :

routes\web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    \Debugbar::info("Hello World !");
    \Debugbar::error('Something went wrong');
    return view('welcome');
});

Now it will show two messages to the debugbar’s messages panel one as info and the second as an error. There are serval methods defined below :

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

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

start_measure('render','Time for rendering');
stop_measure('render');
add_measure('now', LARAVEL_START, microtime(true));
measure('My long operation', function() {
    // Do something…
});

Enabling/Disabling on run time

Sometimes you need to enable or disable the Debugbar during run time. You can use the below methods to do that :

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

Testing Using Laravel Debugbar

As you already set test messages into the main route, you just need to run it into the browser using the below command

php artisan serve

Open this URL in a browser and it will show location information.

http://127.0.0.1:8000

Note: Use the DebugBar only in development. Because it can reveal sensitive information and slow down your Laravel application.