适用于两个用户组的Laravel中间件

时间:2017-07-27 13:06:35

标签: laravel middleware

我有'用户'表两组用户(学生和老师)。 我的路由是:

Route::get ('/students', 'PanelController@showDegrees')>middleware(['auth', 'student'], function () { return "this page requires that you be logged in and an Student";

});

路由:: get(' / students / degrees',' PanelController @ showDegrees')>中间件([' auth','学生& #39;],function(){return"此页面要求您登录并且学生&#34 ;; });

我的中间件:

class Student
{

public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isStudent ()
    {
        return $next($request);
    }

    return redirect('/');

}
}

我的模特用户(这是在项目中):

class User extends Authenticatable
{
use Notifiable;

protected $fillable = [
    'name', 'surname', 'status', 'email', 'password',
];

protected $hidden = [
    'password', 'remember_token',
];

public function isStudent()
{
    return ($this->status == 'STUDENT') ? true : false;
}
}

和内核:

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,
    'student' => \App\Http\Middleware\Student::class
];

问题是它确实有效,但出了点问题......首先,我的Middleawe Auth :: user() - > isStudent()没有看到这个函数,当我将鼠标悬停在这个方法上时它会写\ Illuminate \ Contracts \ Auth \ Athenticatable中找不到...我不知道为什么。

其次,我可以进行重定向,当用户是学生时,然后重定向到路由/学生或/学生/学位?

第三,我的错误来自路由功能("此页面要求您登录并且学生"),我在哪里可以看到这个?

0 个答案:

没有答案