Refer to the tutorial http://laravelacademy.org/post/3502.html when doing front-end and back-end user authentication
I encountered a problem that when a non-logged-in user accesses /admin, it should jump to / admin/login, but I don’t know why it always jumps to /login. Please ask me what is the reason?
PHP中文网2017-05-16 16:55:08
Authenticate under AppHttpMiddleware
return redirect()->guest('admin/login');
That’s it
天蓬老师2017-05-16 16:55:08
Authenticate middleware
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest($guard.'/login'); //注意这里的$guard
}
}
return $next($request);
}
route.php
Route::group(['middleware' => ['auth:admin']], function () { // auth:admin 调用auth中间件的时候传递一个admin,这个admin正好是被中间件的$guard接收,于是访问后台的时候都会被跳转导admin/login,同理 前台用户登陆的中间件可以是 auth:user , 这样会跳转到user/login
Route::get('admin/index', 'AdminController@index');
});