laravel-debugbar: Debugbar::enable() does not seem to work!
I have the below code in a middleware and disabling debug_bar (if i have it enabled by default) works fine! However if I try the opposite, meaning having the debug bar disabled by default and try to enable it on demand then id does not work.
Any ideas?
P.S. lavavel’s own debug mode works fine like this! gets enable or disabled when i want!
if ($request->has('disable_debug')) {
\Config::set('app.debug', false);
\Debugbar::disable();
}
if ($request->has('enable_debug')) {
\Config::set('app.debug', true);
\Debugbar::enable();
}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 3
- Comments: 29 (6 by maintainers)
I have a use case where none of these solutions seem to work:
This is what I ended up doing:
/app/Http/Middleware/DebugBar.php
:/app/Http/Kernel.php
as the last item in the web middleware group.enabled
value needs to benull
in/config/debugbar.php
. Note: I think this still has the issue of the various libraries being loaded on each request, adding a bit of overhead.getIsAdminAttribute()
method to your user model, if that’s your criteria for enabling debugbar.To use debugbar in production, you will need to set
APP_DEBUG
env variable totrue
while debugging things.I have found a workaround however I would prefer to do it with middleware!
I figured that in config/laravel debugbar.php I can do something like
@unitedworx @mikebronner @lvbeck @GoodJob @emilv To enable debugbar in middleware (when it’s off in config and APP_DEBUG=false):
\Barryvdh\Debugbar\Middleware\InjectDebugbar::class
after your middleware (which call\Debugbar::enable()
)app\Http\Kernel.php
:app\Providers\DebugbarEnablerServiceProvider.php
) with this code (which addsweb
middleware in debugbar css/js and other routes)config\app.php
looks like this (Barryvdh\Debugbar\ServiceProvider
must be beforeDebugbarEnablerServiceProvider
)PS. To use debugbar in production, you will need to set APP_DEBUG env variable to false because as written in the documentation:
This method works for activating debugbar for specific IP addresses on production.
I need an advice from people who knows Laravel well, or this my provided way is safe enough. 0.0.0.0 - add your IP you want to activate debugbar
In /config/app.php:
'debug' => env('APP_DEBUG', ($_SERVER['REMOTE_ADDR'] == '0.0.0.0' or $_SERVER['REMOTE_ADDR'] == '0.0.0.0') ? true : false),
In /config/debugbar.php:
'enabled' => null,
In /.env:
Remove line “APP_DEBUG=false”
We wanted to enable Debugbar in production and have the following features:
Achieving this requires you to jump through a few hoops. You can not just set the
APP_DEBUG
environment variable because theenv
helper should only be used in config files when using config caching. Additionally, Debugbar requires a few routes that they add by hooking into the routing engine, which means Debugbar must be enabled when run through CLI.0. Base config
We have Debugbar disabled by default in
config/debugbar.php
:1. Adding policy method
Who should be allowed to toggle Debugbar? Add a policy method for that. Here we chose to add it to our UserPolicy:
2. Setting the cookie
This one is easy. Add the following route:
This cookie must not be encrypted. The reason is that we want to read the cookie before any middlewares later (to enable Debugbar as early as possible). Open
app/Http/Middleware/EncryptCookies.php
and add the cookie to the$except
array:3. Enabling Debugbar when you have the cookie
We enable Debugbar in AppServiceProvider. For us this is early enough. Add this
boot()
method to yourAppServiceProvider
:4. Caching Debugbar routes
To cache Debugbar routes we need to make sure it is enabled during route compilation. Debugbar is disabled in production, but we force it to be enabled in Artisan. Add this method to
app/Console/Kernel.php
:Then create a file
app/Console/BootstrapDebugbar.php
with the following content:5. Ready to use!
All you need to do to use Debugbar is to visit the route you created in step 2. We added it to our menu Blade file:
I have the same issue here, and here’s my way to reillustrate:
config/debugbar.php
setting storage driver toredis
php artisan config:cache
But if I ran
php artisan config:clear
instead ofphp artisan config:cache
, it’ll show up again. So I guess it’s maybe a integration bug with L5.1’s new feature(Config Caching)?