Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel debugger - enable/disable depending on IP address while caching implemented

We need to eneble/disable Laravel debugbar depending on IP address. It works if we clear/disable the caching.

But it does not work when caching enabled. here is my code

//Enabling DEBUGBAR in Production Only for developers
if(in_array($request->ip(), [ip addresses])) {
    config(['app.debug' => true]);
}

.env

APP_DEBUG=false

We are using configuration and route caching. What would be the best way to achieve this?

Laravel version - 5.4

Debugbar version - 2.2

like image 789
Sougata Bose Avatar asked Feb 27 '19 12:02

Sougata Bose


2 Answers

Debugger has functionality to enable/disable it at runtime :

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

If you want to use the debugbar in production, disable in the config and only enable when needed.

So you can do :

if(in_array($request->ip(), [ip addresses])) {
    \Debugbar::enable();
    // Forcing the cache to be cleared
    // Not recommended but if and only if required
    \Artisan::call('cache:clear');
}

Please check documentation for more help.

like image 145
Mihir Bhende Avatar answered Oct 13 '22 16:10

Mihir Bhende


You are using Debugbar library so this library will be loaded before your route or controller loaded, so better you have to bootstrap your stuffs before the library loaded. Then we can bootstrap our custom configuration in the AppServiceProvider class.

Service providers are the central place of all Laravel application bootstrapping.

Simple Method:
Change the file app\Providers\AppServiceProvider.php class according to the below code.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Request;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        // You can also clear cache if needed Artisan::call('cache:clear');
        if(in_array(Request::ip(), ['127.0.0.1'])) {
            config(['app.debug' => true]);
        }
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }
}
like image 30
Googlian Avatar answered Oct 13 '22 17:10

Googlian