简体   繁体   English

登录laravel5.7后重定向到上一页

[英]Redirect to previous page after login laravel5.7

I am working on a requirement where I send emails after record update/insert in which I send page URL as "click here" text, when user clicks on that link he will be redirected to that page regardless of user logs in or not. 我正在制定一项要求,我在发送电子邮件后发送邮件更新/插入,我将页面URL发送为“点击此处”文本,当用户点击该链接时,无论用户是否登录,他都将被重定向到该页面。

Now if the user already logged-in then that page normally opens but if user is not logged-in then I need to redirect back to previous page after login. 现在,如果用户已经登录,那么该页面通常会打开,但如果用户没有登录,那么我需要在登录后重定向回到上一页。

My Problem here is I am unable to redirect back to the previous page which user tried to access before login through 'click here' link from email 我的问题是,我无法重定向回用户尝试访问的上一页,然后通过电子邮件中的“点击此处”链接登录

I have searched for the solutions I found few but they were not working. 我搜索了我发现的解决方案,但他们没有工作。 One of the solution I tried is Scott's answer but din't work why because our application authentication is totally customized. 我尝试的解决方案之一是斯科特的答案,但是没有用,因为我们的应用程序身份验证是完全自定义的。 Our RedirectIfAuthenticated file code is below 我们的RedirectIfAuthenticated文件代码如下

NOTE: Our application has been built on Laravel 注意:我们的应用程序是基于Laravel构建的

RedirectIfAuthenticated.php RedirectIfAuthenticated.php

class RedirectIfAuthenticated
{
  public function handle($request, Closure $next, $guard = null)
  {
    $path = $request->path();
    if (Auth::guard($guard)->check()) {
        if($path == 'app1')
        {
            return redirect('/app1/dashboard');
        }
        elseif($path == 'app2')
        {
            return redirect('/app2/dashboard');
        }
    }

    return $next($request);
  }
}

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

I think you have to redirect on back page 我认为你必须在背页重定向

class RedirectIfAuthenticated
{
  public function handle($request, Closure $next, $guard = null)
  {
    $path = $request->path();
    if (Auth::guard($guard)->check()) {
        return redirect()->back();
    }
  }
}

Try this 尝试这个

override this method in Auth\\LoginController Auth\\LoginController重写此方法

protected function authenticated(Request $request, $user)
{
     if ($request->expectsJson() || $request->ajax()) {
        // return login success in json
     }
     return redirect()->intended($this->redirectPath());
}

Eg. 例如。 if you are trying to access /user/send/email and the user is not logged in then it will redirect to login page once the user is logged in then it will automatically redirect to /user/send/email page 如果您尝试访问/user/send/email并且用户未登录,则一旦用户login后它将重定向到login页面,然后它将自动重定向到/user/send/email页面

RedirectIfAuthenticated RedirectIfAuthenticated

class RedirectIfAuthenticated
{
     public function handle($request, Closure $next, $guard = null)
     {
         $path = $request->path();
         if ( auth()->guard($guard)->check() ) {
            return redirect()->route($path.'.dashboard');
            // or
            // return redirect()->route($guard.'.dashboard'); //Suggest: define route based on guard
        }
        return $next($request);
     }

}

Register route as app1.dashboard and app2.dashboard 将路线注册为app1.dashboardapp2.dashboard

Store the path in session variable, if the user is not logged in. After successful login, check if the session value is set, redirect the user to the value set and before redirecting the user do not forget to unset the session 如果用户未登录,则将路径存储在会话变量中。成功登录后,检查会话值是否已设置,将用户重定向到值集,并在重定向用户之前不要忘记取消设置会话

public function handle($request, Closure $next, $guard = null)
{
        $path = $request->path(); 
        $url = "";
        if($path == 'app1')
        {
           $url="/app1/dashboard";             
         }
        elseif($path == 'app2')
        {
           $url="/app2/dashboard";             
        }

        if (Auth::guard($guard)->check()) {
              return redirect($url);
        }
        else{
          // create session variable and store the path 
          // save `$url` to session variable
        }

      return $next($request);
 }

what you need to do is get the previous URL and check with that and redirect to relevant dashboard you can use URL::previous() in URL Facades. 您需要做的是获取以前的URL并检查并重定向到相关的仪表板,您可以在URL Facades中使用URL::previous() you can do something like this, 你可以做这样的事情,

use Illuminate\Support\Facades\URL;

class RedirectIfAuthenticated
{
   public function handle($request, Closure $next, $guard = null)
   {
      $path1 = url('app1/login')
      $path2 = url('app2/login')

      if (Auth::guard($guard)->check()) {
        if($path1 == URL::previous())
        {
           return redirect('/app1/dashboard');
        }
        elseif($path2 == URL::previous())
        {
            return redirect('/app2/dashboard');
        }
    }

    return $next($request);
  } 
}

Hope this helps. 希望这可以帮助。 if you just want check app1 or app2 I suggest that you explode() the previous() and check 如果你只是想检查app1app2我建议你explode() previous()并检查

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

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