[英]How to Return Redirect Back To Page Before Login | Laravel 8.83.23
I have a laravel app version 8.83.23.我有一个 laravel 应用程序版本 8.83.23。 I have multiple views that have links to show the login page.
我有多个视图,其中包含显示登录页面的链接。 A simple example is this:
一个简单的例子是这样的:
Think of a blog post.想想一篇博文。 "you must login to post a comment".. The user clicks "login" and is taken to the login view (auth.login).
“您必须登录才能发表评论”.. 用户单击“登录”并进入登录视图 (auth.login)。 The user fills the form and submits.
用户填写表格并提交。 The user is authenticated.
用户已通过身份验证。 I then want the authenticated user to be redirected to the page where they originally clicked login.
然后,我希望将经过身份验证的用户重定向到他们最初单击登录的页面。
Is there a simple solution such as changing $redirectTo in the LoginController.php to something dynamic?是否有一个简单的解决方案,例如将 LoginController.php 中的 $redirectTo 更改为动态的东西?
If you want to redirect back to the page where the user logged in如果要重定向回用户登录的页面
return back();
The way I am usually using it is with the Laravel intended
method .我通常使用它的方式是使用Laravel
intended
方法。
The
intended
method provided by Laravel's redirector will redirect the user to the URL they were attempting to access before being intercepted by the authentication middleware.Laravel 的重定向器提供的
intended
方法会将用户重定向到他们在被身份验证中间件拦截之前尝试访问的 URL。 A fallback URI may be given to this method in case the intended destination is not available.如果预期的目的地不可用,则可以为此方法提供后备 URI。
We are setting the url.intended
value in the current session so when the user visits the login page, it will check for the url.intended
to redirect the user where they were when they clicked the login URL. We are setting the
url.intended
value in the current session so when the user visits the login page, it will check for the url.intended
to redirect the user where they were when they clicked the login URL. If there is no url.intended
it will just redirect to the default RouteServiceProvider::HOME
.如果没有
url.intended
它只会重定向到默认的RouteServiceProvider::HOME
。
The AuthenticatedSessionController.php store method is the one that came with the Inertia scaffolding in Laravel 8.83.1 ( not sure if that changed since ) AuthenticatedSessionController.php 存储方法是 Laravel 8.83.1 中的惯性脚手架附带的存储方法(不确定此后是否发生了变化)
MyController.php MyController.php
class QuotesController extends Controller
{
public function productQuote(string $product)
{
if (auth()->guest()) {
session()->put('url.intended', request()->fullUrl());
}
return view(
'get-product-quote',
['product' => $this->getProduct($product), 'user' => $this->getUser()]
);
}
}
And in the get-product-quote
view I simply do this:在
get-product-quote
视图中,我只是这样做:
<x-app-layout>
<div class="relative container mx-auto z-0 py-8 px-4 sm:px-6 lg:px-8">
@guest
<div>
<a :href="route('login')" class="inline-block py-3 px-8 bg-itg-blue hover:bg-itg-blue-dark border border-transparent text-white text-sm rounded-sm text-center">Log in for faster quote submitting process</a>
</div>
@endguest
@auth
...
<p>do the authenticated user stuff</p>
@endauth
</div>
</x-app-layout>
AuthenticatedSessionController.php AuthenticatedSessionController.php
class AuthenticatedSessionController extends Controller
{
/**
* Handle an incoming authentication request.
*
* @param \App\Http\Requests\Auth\LoginRequest $request
* @return \Illuminate\Http\RedirectResponse
*/
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect()->intended(RouteServiceProvider::HOME);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.