我有一个简单的用户表,我通过在类型列中输入1来定义和管理。
我在我的中间件中进行了以下设置,但这仍然不能阻止非管理员访问仅限管理员的区域。
中间件:
<?php
namespace App\Http\Middleware;
use Closure;
class Admin {
public function handle($request, Closure $next)
{
if (Auth::user()->isAdmin())
{
return redirect('home');
}
return $next($request);
}
}
籽粒:
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,
];
路线:
Route::group(['middleware' => 'auth', 'admin'], function () {
Route::get('admin/dashboard', 'AdminController@dashboard');
Route::get('admin/orders', 'AdminController@orders');
});
我的用户类中的功能:
public function isAdmin()
{
if (Auth::user()->type == '1')
{
return true;
}
else
{
return false;
}
}
答案 0 :(得分:2)
您需要将多个中间件作为数组传递:
Route::group(['middleware' => ['auth', 'admin']]