Laravel Debugbar Not Showing? How to Make It Work and Troubleshoot Effectively
The Laravel Debugbar is a powerful debugging tool that provides insights into requests, database queries, routes, and more. However, sometimes it might not work as expected out of the box. In this article, I’ll show you how to ensure Debugbar works correctly by creating a custom CustomDebugbarServiceProvider.
Step 1. Install Debugbar via Composer
First, you need to install Laravel Debugbar. Run the following command in your terminal:
composer require barryvdh/laravel-debugbar --dev
This will add Debugbar to the require-dev section of your composer.json file.
Here’s an example of the require-dev section after installation:
"require-dev": { "barryvdh/laravel-debugbar": "^3.7", "facade/ignition": "^2.5", "mockery/mockery": "^1.4", "phpunit/phpunit": "^9.0" }
Step 2. Publish Debugbar Configuration
To configure Debugbar, publish its configuration file by running:
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"
This will create a debugbar.php file in your config folder, where you can customize the tool’s behavior.
Step 3. Create a CustomDebugbarServiceProvider
If Debugbar still doesn’t work, you might need to create a custom service provider to adjust its behavior. Follow these steps:
1. Create a new file called CustomDebugbarServiceProvider.php in the app/Providers directory
Here’s an example of the file’s content:
<?php namespace App\Providers; use Barryvdh\Debugbar\ServiceProvider as DebugbarServiceProvider; use Illuminate\Support\Facades\URL; class CustomDebugBarServiceProvider extends DebugbarServiceProvider { public function boot() { parent::boot(); // Принудительно установите базовый URL с нужным портом if (config('app.debug') && app()->environment('local')) { URL::forceRootUrl('http://localhost:8081'); } } }
This code extends the default DebugbarServiceProvider and adds logic to set the base URL for the local environment.
Step 4. Register the CustomDebugbarServiceProvider
Open the config/app.php file and add your custom service provider to the providers array:
'providers' => [ /* * Laravel Framework Service Providers... */ Illuminate\Auth\AuthServiceProvider::class, Illuminate\Broadcasting\BroadcastServiceProvider::class, Illuminate\Bus\BusServiceProvider::class, Illuminate\Cache\CacheServiceProvider::class, Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class, Illuminate\Cookie\CookieServiceProvider::class, Illuminate\Database\DatabaseServiceProvider::class, Illuminate\Encryption\EncryptionServiceProvider::class, Illuminate\Filesystem\FilesystemServiceProvider::class, Illuminate\Foundation\Providers\FoundationServiceProvider::class, Illuminate\Hashing\HashServiceProvider::class, Illuminate\Mail\MailServiceProvider::class, Illuminate\Notifications\NotificationServiceProvider::class, Illuminate\Pagination\PaginationServiceProvider::class, Illuminate\Pipeline\PipelineServiceProvider::class, Illuminate\Queue\QueueServiceProvider::class, Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, Illuminate\Translation\TranslationServiceProvider::class, Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, /* * Package Service Providers... */ /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, <strong>App\Providers\CustomDebugBarServiceProvider::class</strong> ],
Step 5. Verify Debugbar Is Working
1. Ensure debugging is enabled in your .env file or in config/app.php:
APP_DEBUG=true
2. Start your local development server with the correct host and port:
php artisan serve --host=localhost --port=8081
3. Open your application in a browser at http://localhost:8081 and check if the Debugbar is displayed.
Step 6. Clear Configuration and Cache
If the Debugbar is still not showing, clear your application configuration and cache:
php artisan config:clear
php artisan cache:clear
Refresh the browser to verify that Debugbar is now visible.
Conclusion
Creating a custom CustomDebugbarServiceProvider allows you to control Debugbar’s configuration and resolve issues related to its initialization. This is especially helpful in a local environment where you may need to set a custom base URL or port. Now, you know how to enable Debugbar in Laravel and customize its behavior to fit your needs.