R
R
Ruslan Yanborisov2018-12-19 14:05:58
Laravel
Ruslan Yanborisov, 2018-12-19 14:05:58

How to enable debug laravel for admins only?

I want to disable debug in the browser for all users except the admin, I did everything according to this instruction , but something does not work.
Fragment config/app.php (I have disabled Debug Mode):

/*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */

    'debug' => false,

app/Http/Middleware/OnEnter:
namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class OnEnter
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
  {
    if (Auth::user()->moderated == '8') //Так у меня идет проверка админа 
    {
      Config::set('app.debug', true);
    }
  }
}

Fragment app/Http/Kernel.php:
/**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'enter' => \App\Http\Middleware\OnEnter::class,
    ];

Now, when an error occurs, an error page is simply displayed, that is, not Debug Mode, in short, I need Debug Mode to work for users with moderated = 8.
Thanks to!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rikcon, 2018-12-19
@Rus_K_o

Throw it not in the routeMiddleware (everything in the routeMiddleware is called only by hand from the route or controller)
As I understand it, you need the $middleware array which is higher, then the mildware is executed on EVERY request.
But I think it's better to throw it in the web group

protected $middlewareGroups = [
        'web' => [ вот сюда

PS If you have an old version of Laravel (it seems to be up to 5.2), then you don’t have such a group, put it in $middleware then or write it to the necessary routes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question