Laravel 5中用于管理员或身份验证的Laravel中间件 [英] Laravel middleware for admin or auth in laravel 5
本文介绍了Laravel 5中用于管理员或身份验证的Laravel中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是laravel的新手,不了解laravel限制机制,我已经阅读了有关中间件的信息,但对如何使用它以及为什么使用它以及它将如何工作感到困惑,所以请指导我如何实现它限制目的,即用于身份验证,即用户路由.
i am new to laravel and don't know about laravel restriction mechanism, i have read about middleware but confused how to use it and why it is used and how this will works, so please guide me how i can implement it for restriction purposes i.e for auth, sa user routes.
推荐答案
确保您在数据库用户表中具有角色列或属性.
Make Sure your have role column or attribute in database users table.
STEP 1
创建Midlleware
Create a Midlleware
php artisan make:middleware AnyNameYouWant
它将为您创建一个不错的样板.
it will create a nice boilerplate for you.
STEP 2
public function handle($request, Closure $next)
{
if (\Auth::user()->role == 'admin') {
return $next($request);
}
return redirect('home');
}
第3步
在内核中使用
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,
'admin' => \App\Http\Middleware\YourMiddleware::class,
];
第4步
保护您的路线.
Route::get('admin/profile', function () {
//
})->middleware('admin');
您完成了
这篇关于Laravel 5中用于管理员或身份验证的Laravel中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文