Php laravel的多重身份验证

Php laravel的多重身份验证,php,laravel,laravel-5.3,Php,Laravel,Laravel 5.3,我想将中间件auth拆分为两个角色,一个是管理员角色,另一个是用户角色 但有些路由是供所有用户和管理员使用的,只有少数路由是供管理员使用的。我怎样才能使用路由进行拆分 Auth::routes(); Route::group(['middleware' => 'auth'], function () { //Some route here }); Route::group(['middleware' => ['guest']], function ()

我想将中间件auth拆分为两个角色,一个是管理员角色,另一个是用户角色 但有些路由是供所有用户和管理员使用的,只有少数路由是供管理员使用的。我怎样才能使用路由进行拆分

Auth::routes();
Route::group(['middleware' => 'auth'], function () {        
     //Some route here 
});

Route::group(['middleware' => ['guest']], function () {
   //some route here
});

这是我对管理员和用户(在我的例子中是代理)访问控制的实现。我的用户表(
is_admin
)中有一个布尔字段,普通用户为0,管理员为1

在您的用户模型中添加以下内容:

protected $casts = [
    'is_admin' => 'boolean',
];

public function isAdmin()
{
    return $this->is_admin;
}
php artisan make:middleware Admin

php artisan make:middleware Agent
public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/agent');
}
    public function __construct()
{   

    $this->middleware('auth');
    $this->middleware('admin');
}
public function __construct() {

$this->middleware('auth');
$this->middleware('agent');

}
为管理员和代理创建新的中间件:

protected $casts = [
    'is_admin' => 'boolean',
];

public function isAdmin()
{
    return $this->is_admin;
}
php artisan make:middleware Admin

php artisan make:middleware Agent
public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/agent');
}
    public function __construct()
{   

    $this->middleware('auth');
    $this->middleware('admin');
}
public function __construct() {

$this->middleware('auth');
$this->middleware('agent');

}
中间件文件将在
App\Http\middleware\

将其添加到
Admin.php中的类中

protected $casts = [
    'is_admin' => 'boolean',
];

public function isAdmin()
{
    return $this->is_admin;
}
php artisan make:middleware Admin

php artisan make:middleware Agent
public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/agent');
}
    public function __construct()
{   

    $this->middleware('auth');
    $this->middleware('admin');
}
public function __construct() {

$this->middleware('auth');
$this->middleware('agent');

}
将其添加到
Agent.php

public function handle($request, Closure $next)
{    
    if ( Auth::check() && !Auth::user()->isAdmin() )
    {
        return $next($request);
    }    
    return redirect('/home');
}
在此之后,向laravel注册您的中间件,将其添加到位于
app\Http\Kernel.php的
Kernel.php中的
protected$routeMiddleware

'admin' => 'App\Http\Middleware\Admin',
'agent' => 'App\Http\Middleware\Agent',
确保为重定向创建正确的路由,正如我们在中间件文件中提到的那样。在这之后,你几乎完成了。现在要验证用户是管理员还是普通用户,请将其添加到控制器的构造函数方法中

仅允许管理员用户执行的操作:

protected $casts = [
    'is_admin' => 'boolean',
];

public function isAdmin()
{
    return $this->is_admin;
}
php artisan make:middleware Admin

php artisan make:middleware Agent
public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/agent');
}
    public function __construct()
{   

    $this->middleware('auth');
    $this->middleware('admin');
}
public function __construct() {

$this->middleware('auth');
$this->middleware('agent');

}
仅允许普通(代理)用户执行的操作:

protected $casts = [
    'is_admin' => 'boolean',
];

public function isAdmin()
{
    return $this->is_admin;
}
php artisan make:middleware Admin

php artisan make:middleware Agent
public function handle($request, Closure $next)
{
    if ( Auth::check() && Auth::user()->isAdmin() )
    {
        return $next($request);
    }
    return redirect('/agent');
}
    public function __construct()
{   

    $this->middleware('auth');
    $this->middleware('admin');
}
public function __construct() {

$this->middleware('auth');
$this->middleware('agent');

}
或者您也可以将中间件添加到路由中,

Route::group(['middleware' => 'admin'], function () {        
     //Some route here 
});
检查唯一的方法