目前要添加身份验证,我在路由文件中使用它:
Route::middleware(['auth'])->group(function () {
});
但是如果用户是管理员,我还想检查不同的路由,所以目前我这样做并且有一个自定义中间件文件:
Route::middleware(['auth', 'admin'])->group(function () {
});
//
<?php
namespace App\Http\Middleware;
use Closure;
class Admin {
public function handle($request, Closure $next)
{
if ( Auth::check() && Auth::user()->isAdmin() )
{
return $next($request);
}
return redirect('dashboard');
}
}
这一切都运行正常,但我注意到它使用api中间件:
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
问题
'auth:admin'
'auth:api'
中间件的文件位于何处,我在app文件夹中找不到它config/auth.php
,然后在两个表之间分隔用户,一个用于管理员,另一个用于其他用户。答案 0 :(得分:0)
auth:api
实际上是基本的auth
中间件,字符串api
作为参数。这意味着用户为authenticated using the api
authentication guard
如果您为管理员添加了a custom authentication guard,则可以使用auth:admin
。
答案 1 :(得分:0)
auth
中间件接受一个参数,即要使用的防护。正如您在Illuminate\Auth\Middleware\Authenticate
中间件中所看到的那样。
您可以添加custom auth guards。因此,如果您愿意,可以创建auth:admin
。但我确实认为使用一个中间件来验证用户是谁(身份验证)并且第二个验证用户是否被允许访问他/她想要访问的页面(授权)是完全没错的。