1
我想要管理員授權。所以我在中間件有新類Laravel 5:我應該如何實現Auth :: admin()作爲Auth :: guest()?
class Admin {
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->isAdmin())
{
return $next($request);
}
Session::flash('message', 'You need to be an administrator to visit this page.');
return redirect('/');
}
}
然後在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\Admin::class, //added line
];
我也定義isAdmin()註冊。它的工作原理我做這個時,在路線:
get('protected', ['middleware' => ['auth', 'admin'], function() {
return "this page requires that you be logged in and an Admin";
}]);
但我想使用它像驗證::管理員()作爲驗證::客(),我應該在哪裏實現這個功能呢?我需要在Guard.php中的抽象admin()嗎?
我可以解決Auth :: user() - > isAdmin(),但我仍然想知道正確的方法來做到這一點。
謝謝。
謝謝。
我可以知道你想實施這個嗎? –
我想在你的控制器的網頁 – daolincheng
做Auth :: admin()? –