Php 如何将laravel 5.1中间件参数用于多个身份验证和受保护路由?
我是拉威尔5.1的新手。 我如何使用中间件参数保护我的管理路由不受用户攻击? 大概是这样的:Php 如何将laravel 5.1中间件参数用于多个身份验证和受保护路由?,php,authentication,middleware,laravel-5.1,laravel-middleware,Php,Authentication,Middleware,Laravel 5.1,Laravel Middleware,我是拉威尔5.1的新手。 我如何使用中间件参数保护我的管理路由不受用户攻击? 大概是这样的: Route::group(['middleware' => 'auth:admin'], function() /* Admin only Routes*/ { ////// }); 我在users表中有一个字段角色,该字段角色获得两个值: 1为管理员 2适用于用户 在我的应用程序中,用户有其受保护的路由。 我不想使用软件包。您可以这样做。注入Guard类,然后使用它检查用户。您实
Route::group(['middleware' => 'auth:admin'], function()
/* Admin only Routes*/
{
//////
});
我在users表中有一个字段角色,该字段角色获得两个值:
1为管理员
2适用于用户
在我的应用程序中,用户有其受保护的路由。
我不想使用软件包。您可以这样做。注入Guard类,然后使用它检查用户。您实际上不需要传递参数。只需将中间件命名为“admin”或其他名称。下面的中间件将检查当前用户的角色是否为admin,如果不是,则重定向到另一个路由。在失败时,你可以做任何你喜欢做的事
<?php
namespace Portal\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Guard;
class Admin
{
/**
* The Guard implementation.
*
* @var Guard
*/
protected $auth;
/**
* Create a new filter instance.
*
* @param Guard $auth
*/
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($this->auth->user()->role != 'admin') {
return redirect()->route('not-an-admin');
}
return $next($request);
}
}
public function handle($request, Closure $next, $role)
{
if($this->auth->user()->role != $role) {
return redirect()->route('roles-dont-match');
}
return $next($request);
}