A
A
Andrey Boychenko2017-03-21 05:09:11
Laravel
Andrey Boychenko, 2017-03-21 05:09:11

middleware options?

Good night, I don’t understand why what is written on the documentation site does not work for me ... I
use standard Laravel authentication, but in addition to everything, there are routes that should be available only to users with specific roles. Based on the documentation, I wrote the following
created the hasRole method in the User model

public function hasRole()
    {
      return $this->role;
    }

Created a mediator and gave it a name in Karnel.php
mediator
public function handle($request, Closure $next, $role)
    {
        if (!Auth::user()->hasRole($role)) 
        {
            return redirect(abort(403));
        }
        return $next($request);
    }

Karnel.php
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,

        'role' => \App\Http\Middleware\role::class,
    ];

and the actual file and routes
$router->group(['prefix'=>'/dashboard', 'middleware'=>'auth'], function($router)
{
    $router->group(['middleware'=>"role:administrator"], function(){
        Route::get('/admin', function(){
            print "Administrators part here";
        });
        //some admin routes

    });

    $router->group(['middleware'=>"role:client"], function(){
        Route::get('/client', function(){
            print "Client part here";
        });
        //some client routes
    });
});

And in this form, it lets me into any of the routes, regardless of the role, what to do and where to look? Thank you!

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