Show Laravel Debugbar to specific Users

Laravel Debugbar is a valuable debugging tool that provides detailed insights into your Laravel application’s performance and functionality. By default, it is enabled for all users in the development environment. However, there might be cases where you want to restrict access to Debugbar and enable it only for specific users, such as administrators.

In this article, we will see some practical examples for enabling Laravel Debugbar for admin users only using middleware, ensuring a seamless debugging experience while maintaining the security and privacy of your application.

Install the Laravel Debugbar Package

Ensure that you have Laravel installed and configured in your development environment. Open your terminal and navigate to your Laravel project’s directory. Run the following command to install the Laravel Debugbar package:

composer require barryvdh/laravel-debugbar --dev

Create a Custom Middleware

Next, we will create a custom middleware that will control access to Debugbar based on user authentication. In your terminal, run the following command to generate the middleware:

php artisan make:middleware DebugbarAccessMiddleware

This will create a new file named DebugbarAccessMiddleware.php in the app/Http/Middleware directory.

Modify the Middleware Logic

Open the Middleware file in your preferred text editor. Inside the handle() method, update the logic to allow access to the Debugbar for specific users. You can use any criteria you prefer, such as user roles, email addresses, or IDs. Here’s an example that allows access only to users with is_admin flag:

<?php

namespace App\Http\Middleware;

use Closure;
use Debugbar;

class DebugbarAccessMiddleware
{
    public function handle($request, Closure $next)
    {
        $user = $request->user();

        if ($user && $user->is_admin) {
            Debugbar::enable();
        } else {
            Debugbar::disable();
        }

        return $next($request);
    }
}

Register the Middleware

To make Laravel recognize the newly created middleware, open the app/Http/Kernel.php file and locate the $routeMiddleware array. Add the following entry to the array:

'debugbar' => \App\Http\Middleware\DebugbarAccessMiddleware::class,

Apply the Middleware to Routes

Now, you can apply the debugbar middleware to the routes or controllers that require Debugbar access. For example, if you want Debugbar enabled for a specific route, you can define it in your routes/web.php file as follows:

Route::middleware('debugbar')->get('/debug', function () {
    // Your route logic here
});

Alternatively, you can apply the middleware to a controller method by adding it to the controller’s constructor or to specific methods using the middleware method.

Conclusion

Enabling Laravel Debugbar for specific users using middleware allows you to control access to this powerful debugging tool, ensuring that it is available only to selected individuals. Here, we have taken an example for enabling Laravel Debugbar for admin users only. However, you can customize logic as per your requirement.