I
I
Ivan2022-01-31 12:53:17
Laravel
Ivan, 2022-01-31 12:53:17

Laravel how to enable debugbar for admins only?

Created a middleware called DebugForAdmins

Here is its code:

public function handle(Request $request, Closure $next, $guard = null)
    {
        $auth = Auth::guard($guard);

        if ($auth->user()->isAdmin()) {
            \Debugbar::enable();
        }

        return $next($request);
    }


Now basically I need to include it in Kernel.php:
\App\Http\Middleware\DebugForAdmins::class,
There are two options where I can add this line:
1. In $middlewareGroups in 'web' - but then the code just doesn't work. Those. the admin user is successfully determined, but the debugbar simply does not turn on.
2. In $middleware - then I can just call \Debugbar::enable() in my DebugForAdmins without any checks and everything will work. But for all users. But if I add checks for the admin, the code will stop working, because the session connects later and at this stage I still can't check if the user is an admin.

Those. here, the session starts only in $middlewareGroups in 'web', so my DebugForAdmins middelweir needs to be included below it as well. But in this case, as I said above, \Debugbar::enable() simply doesn't work.
\Illuminate\Session\Middleware\StartSession::class,


Bottom line: In one case, I can enable DebugBar, but I can not check the user for admin. In the second case, I can check the user for an admin, but I simply cannot enable the DebugBar.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question