我想根据已记录的用户和访客用户对Laravel 5路径进行分组。 Laravel 5中是否有任何内置的框架方法来执行此操作?
答案 0 :(得分:6)
是的,有一些:https://laravel.com/docs/master/middleware#assigning-middleware-to-routes auth
代表授权,guest
代表来宾。
Route::group(['middleware' => ['auth']], function () {
//only authorized users can access these routes
});
Route::group(['middleware' => ['guest']], function () {
//only guests can access these routes
});
答案 1 :(得分:1)
是的,您可以通过在Authenticate.php中更新以下方法来完成此操作
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
如果您使用的是Sentinel,则可以从
中查看已登录的用户 Sentinel::check()
代替Auth::guard($guard)->guest()
然后您可以按如下方式对路径进行分组。
Route::group(['middleware' => ['auth']], function () {
// Authorized routs
});
Route::group(['middleware' => ['guest']], function () {
// Guest routs
});