laravel重定向(如果已登录) [英] laravel redirect if logged in

查看:152
本文介绍了laravel重定向(如果已登录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel 5.1.8. 我正在制作一个登录/注册系统. 我制作了一个名为AdminController的控制器,并使用中间件对其进行了保护.

i am using laravel 5.1.8. i am making a login/registration system. i made a controller named AdminController and protect it with middleware.

但是我正在使用laravel的默认AuthController,这些方法和类位于不同的位置.路线在哪里:

but i am using laravel's default AuthController which methods and classes are located in different locations. where routes are:

Route::Controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController'
]);

get('admin', 'AdminController@index');
get('profile', 'AdminController@profile');
get('article', 'AdminController@article');

用户不登录就无法访问AdminController.它已重定向到登录页面.但是我想要,如果一个已登录的用户在浏览器的地址栏上键入登录页面或注册的地址,该页面将被重定向到AdminController.

users cannot access AdminController without logging in. its redirected to login page. but i want, if a logged in user typed the address of login page or registration on the address bar of browser, the page will redirected to AdminController.

当我尝试执行此操作时,它会查找"/home"并给出错误.我想将其设置为"/admin".

when i try to do this, it looking for '/home' and gives errors. i want to make it '/admin'.

推荐答案

转到 App\Http\Middleware\RedirectIfAuthenticated 然后从

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        return redirect('/home');
    }

    return $next($request);
}

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        return redirect('/admin');
    }

    return $next($request);
}

这篇关于laravel重定向(如果已登录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆