简体   繁体   English

如果登录,laravel 重定向

[英]laravel redirect if logged in

i am using laravel 5.1.8.我正在使用 Laravel 5.1.8。 i am making a login/registration system.我正在制作一个登录/注册系统。 i made a controller named AdminController and protect it with middleware.我制作了一个名为 AdminController 的控制器并用中间件保护它。

but i am using laravel's default AuthController which methods and classes are located in different locations.但我使用的是 laravel 的默认 AuthController,其中的方法和类位于不同的位置。 where routes are:其中路线是:

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

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

users cannot access AdminController without logging in. its redirected to login page.用户不能在没有登录的情况下访问 AdminController。它被重定向到登录页面。 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.但我想,如果登录用户在浏览器的地址栏上输入登录页面或注册的地址,页面将重定向到 AdminController。

when i try to do this, it looking for '/home' and gives errors.当我尝试这样做时,它会寻找“/home”并给出错误。 i want to make it '/admin'.我想让它'/admin'。

go to App\\Http\\Middleware\\RedirectIfAuthenticated then change it from转到App\\Http\\Middleware\\RedirectIfAuthenticated然后将其从

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

    return $next($request);
}

to

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

    return $next($request);
}

Add this to your AuthController :将此添加到您的AuthController

protected $redirectTo = '/admin';

This tells all the redirect methods in the various traits to redirect there instead of to /home .这告诉各种特征中的所有重定向方法重定向到那里而不是/home

Include \\App\\Http\\Middleware\\RedirectIfAuthenticated::class middleware in $middlewareGroups "web" array after \\Illuminate\\Session\\Middleware\\StartSession::class middleware\\Illuminate\\Session\\Middleware\\StartSession::class中间件之后的 $middlewareGroups "web" 数组中包含\\App\\Http\\Middleware\\RedirectIfAuthenticated::class \\Illuminate\\Session\\Middleware\\StartSession::class中间件

then modify your redirect path in handle() method of RedirectIfAuthenticated然后在 RedirectIfAuthenticated 的handle()方法中修改重定向路径

public function handle($request, Closure $next, $guard = null)
    {
        //check if authenticate && second is condition when we need to redirect i.e, 
         if(Auth::guard($guard)->check() && $request->route()->named('login') ) {
        return redirect()->route('dashboard');
    }

        return $next($request);
    }

You can check using auth function.您可以使用 auth 功能进行检查。

public function checkLogin()
{

    if (auth()->user()) 
    {
         return redirect(route('home'));
    }
    else
    {
           return redirect(route('login'));
    }
}

when a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle.当用户成功通过身份验证时,他们将被重定向到 /home URI,您需要注册一个路由来处理。 You can customize the post-authentication redirect location by defining a redirectPath property on the AuthController:您可以通过在 AuthController 上定义 redirectPath 属性来自定义身份验证后重定向位置:

protected $redirectPath = '/dashboard';受保护的 $redirectPath = '/dashboard';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM