dimanche 11 mars 2018

Create superAdmin middleware in Laravel 5.1

I need to create SuperAdmin middleware in my app called 'master' so I make middleware 'IsMaster' :

<?php

namespace App\Http\Middleware;

use Closure;

class IsMaster
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (\Auth::user() &&  \Auth::user()->admin == 101) {
            return $next($request);
        }

        return redirect('/auth/logout');
    }
}

so as you can see from the code above if 'admin' field in my 'users' table has value 101 that is MASTER user.

Now I add this to Kernel.php:

protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'admin' => \App\Http\Middleware\IsAdmin::class,
        'master' => \App\Http\Middleware\IsMaster::class,

    ];

now at AdminController.php I use it like:

class AdminController extends Controller
{

    public function __construct() {
        $this->middleware('master', ['except' => ['getMail']]);
    }

...

when I try to login with master user account I got error:

ReflectionException in Container.php line 741: Class master does not exist

enter image description here

What is a problem here? I really dont see what make the issues...

Also I try: php artisan config:clear, php artisan cache:clear ...



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire