登录后,根据引用路由将用户重定向到特定路由



AuthController.php中,我有一个函数:

public function authenticated( IlluminateHttpRequest $request, AppUser $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(IlluminateHttpRequest $request, AppUser $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 参数访问检查是否存在。就是这样,登录后重定向到页面,否则,转到指定的页面。

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 );
}

即使您在网址中使用参数,这也有效

最新更新