Home » Php » php – How to show laravel debugbar only to certain people?

php – How to show laravel debugbar only to certain people?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I use barryvdh/laravel-debugbar. How can I display debugbar only for certain people?

How to&Answers:

Check the documentation here: https://github.com/barryvdh/laravel-debugbar

If you want to enable/disable debugbar at runtime, use this codes:

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

You may do something like this. Create a middleware php artisan make:middleware TestMiddleware, Don’t forget to edit app/Http/Kernel.php and add TestMiddleware

protected $middleware = [
        ...
        \App\Http\Middleware\TestMiddleware::class,
        ...
    ];

Then this is your TestMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;

class TestMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (auth()->user() && in_array(auth()->id(), [1,2,3])) {
            \Debugbar::enable();
        }
        else {
            \Debugbar::disable();
        }

        return $next($request);
    }
}

Answer:

You can do this in this in Your AppServiceProvider.php

namespace App\Providers;
use Barryvdh\Debugbar\ServiceProvider as Bry;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {


    }
   public function register(){

      if($_SERVER['REMOTE_ADDR'] == 'YourIp'){
         $this->app->register(Bry::class); // or $this->app->register(new  Barryvdh\Debugbar\ServiceProvider()); 
       }

   }

By this way you can register ServiceProvider dynamicaly.

Hope this helps.

Answer:

You can check for IP address to toggle it in web.php.

//Enabling DEBUGBAR in Production Only for developers
if(in_array(Request::ip(), [allowed IPs])) {
    config(['app.debug' => true]);
}