[英]Redirect user to a specific route based on the referring route after login
在
AuthController.php
我有一个函数:
public function authenticated( \\Illuminate\\Http\\Request $request, \\App\\User $user ) { return redirect()->intended($this->redirectPath()); }
我在每个页面上都有登录弹出窗口,因此,如果用户从以下页面登录:
/demo-page
并登录,登录后它们将被重定向回同一页面(
/demo-page
)。
这完全可以预期。
没问题
我的问题:如何根据用户当前从中调用登录的路由将用户重定向到特定页面。
逻辑如下:
if referring route('indexpage'){ redirect to route('homepage'); } else any other route other than index page{ return redirect()-> to previous url }
所以以前我在看我的AuthController
文件。 但是我忘记了我已经更改了LoginController
的重定向
public function __construct() { $this->redirectTo = url()->previous(); //Redirect the user to previous page after login $this->middleware('guest')->except('logout'); }
我需要做什么:
假设有3条路线。 A,B和C
首先,用户登陆页面A,如果他登录到该页面,则必须将其重定向到页面B。
如果用户在页面B上并且在该页面登录,则必须将其重定向回页面B。
如果用户在页面C上并在该页面登录,则必须将其重定向回页面C。
注意:登录名是每个页面标题上的常见形式。
目前在做什么?
使用return redirect()->intended($this->redirectPath());
为此,用户从A重定向到A,B重定向到B,C重定向到C。
这应该工作:
public function authenticated(\Illuminate\Http\Request $request, \App\User $user)
{
if ($to = $request->input('redirect_route')) {
$to = URL::route($to);
}
$to = $to ?: $this->redirectPath();
return redirect()->intended(
$to
);
}
在此处阅读API。
您可以在将访问权限限制为自动访问的用户的页面上执行以下操作,使用此代码设置称为accesscheck的url参数:
$restrictGoTo = "login.php";
if (!((isset($_SESSION['Username'])) &&
(isAuthorized("",$authorizedUsers, $_SESSION['Username'],
$_SESSION['UserGroup'])))) {
$qsChar = "?";
$referrer = $_SERVER['PHP_SELF'];
if (strpos($restrictGoTo, "?")) $qsChar = "&";
if (isset($_SERVER['QUERY_STRING']) && strlen($_SERVER['QUERY_STRING']) >
0)
$referrer .= "?" . $_SERVER['QUERY_STRING'];
$restrictGoTo = $restrictGoTo. $qsChar . "accesscheck=" .
urlencode($referrer);
header("Location: ". $restrictGoTo);
}
然后在您的登录页面上。 您需要做的第一件事是检查url参数accesscheck是否存在。 是的,登录后重定向到该页面,否则,请转到指定的页面。
if (isset($_GET['accesscheck'])) {
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}
//run your login script and if the user is authenticated proceed to the
previous url or the success page:
$redirectLoginSuccess = "yourpage.php";
if (isset($_SESSION['PrevUrl']) && true) {
$redirectLoginSuccess = $_SESSION['PrevUrl'];
}
header("Location: " . $MM_redirectLoginSuccess );
}
即使您在网址的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.