简体   繁体   English

在Laravel 5中登录该页面后如何在登录后重定向回上一页

[英]How to redirect back after logged-in to previous page after logged out on that page in Laravel 5

I am using Laravel 5.3 , I have a situation here...I'm on PAGE-2 and logged-out from there then redirected to Login Page. 我正在使用Laravel 5.3 ,这里有一种情况...我在PAGE-2上并从那里注销,然后重定向到登录页面。 Now, what I am trying to achieve is to redirect back to PAGE-2 if the user logs-in again. 现在,我试图实现的是如果用户再次登录,则重定向回PAGE-2。

the Current situation is that, the user will be redirected to defaultAfterLogin page, which is not my desired login flow. 当前的情况是,用户将被重定向到defaultAfterLogin页面,这不是我想要的登录流程。

NOTE: Default page after-login is " DashBoard ". 注意:登录后的默认页面是“ DashBoard ”。

It is OKAY IF YOU WILL GO THE FIRST-TIME TO PAGE-2(not the default DashBoard page) and if you're not LOGGED-IN you will be redirected to LOGIN PAGE then IF YOU'll login again you will be redirected back to PAGE-2, which is fine. 可以,如果您要第一次进入PAGE-2(不是默认的DashBoard页面),并且如果您未登录,将被重定向到LOGIN PAGE,然后如果您再次登录,将被重定向回到PAGE-2,这很好。

BUT what is happening now is that, when you're in PAGE-2 then you LOGOUT then you will be REDIRECTED TO LOGIN-PAGE, if you LOGIN again you will be redirected to "DashBoard" which is not what I want. 但是现在发生的是,当您进入PAGE-2时,您将注销,然后您将被重定向到LOGIN-PAGE,如果再次登录,您将被重定向到“ DashBoard”,这不是我想要的。 It should redirect back to PAGE-2 它应该重定向回PAGE-2

The flow should be,... users will be redirected after login no matter which PAGE they're previously working with. 该流程应为:...用户登录后将被重定向,无论他们之前使用哪个PAGE。

Here's the a sample script I am using (it's actually from laravel i'am using its built-in Auth) 这是我正在使用的示例脚本(实际上是来自laravel,我正在使用其内置的Auth)

protected function handleUserWasAuthenticated(Request $request, $throttles)
    {
        if ($throttles) {
            $this->clearLoginAttempts($request);
        }

        if (method_exists($this, 'authenticated')) {
            return $this->authenticated($request, Auth::guard($this->getGuard())->user());
        }

        return redirect()->intended($this->redirectPath());
    }

Any ideas, please? 有什么想法吗? Thank you very Much for your help. 非常感谢您的帮助。

Try this On auth middleware:app/http/middleware/RedirectIfAuthenticated 在auth中间件上尝试此操作:app / http / middleware / RedirectIfAuthenticated

// redirect the user to your login page "/login" //将用户重定向到您的登录页面“ / login”

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

  return $next($request);
}

// This is your login method //这是您的登录方法

public function postSignIn(Request $request)
  { 
    $request_data = $request->all();
    $email = $request_data['email'];
    $user_details = User::where('email', $email)->first();
    if(count($user_details) > 0)
    {
      $credentials = array('email'=> $email ,'password'=> $request_data['password']);        
      if ($this->auth->attempt($credentials, $request->has('remember')))
      {
        return redirect()->to('/dashboard'); //Here is your redirect url, redirect to dashbord
          OR
        return redirect()->to('/page2'); //Here is your redirect url, redirect to page2
      }
      else
      {
        $error = array('password' => 'Please enter a correct password');
        return redirect()->back()->withErrors($error); 
      }
    }
    else
    {
      $error = array('password' => 'User not found');
      return redirect()->back()->withErrors($error);  
    }
  }

Open AuthController class : app/Http/Controllers/Auth/AuthController.php 打开AuthController类:app / Http / Controllers / Auth / AuthController.php

Add below property to the class 将以下属性添加到类中

protected $redirectAfterLogout = 'auth/login';

you can change auth/login with any url. 您可以使用任何网址更改身份验证/登录。

U CAN USE THIS CODE 您可以使用此代码

       return redirect()->back();

or 要么

u can use route and in ur route u need to configure and in controller u can put the below code // this is ur route 您可以使用路由,在您的路由中,您需要配置并在控制器中,您可以输入以下代码//这是您的路由

Route::get('/dashboard',[
'uses' => 'PostController@dashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);

//put this in ur controller return redirect()->route('dashboard'); //将其放入您的控制器中return return()-> route('dashboard');

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

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