在Laravel应用程序中的页面重定向后,会话不会被保存
成功登录检查并创建会话后,我将重定向页面,但是页面重定向后不可用。
我的登录控制器:
$validated = $request->validate([
'email' => 'required|email',
'password' => 'required',
'g-recaptcha-response' =>'required|captcha',
],
[
"g-recaptcha-response.required" => "Captcha is required!",
]);
if(usernamd and password matches){
session()->put("username",$username);
return redirect()->route('home');
}else{
return Redirect::to("/login")->withFail('Wrong Passoword entered.');
}
如果我打印会话变量,它们是可见的并且具有正确的值,但是当将其重定向到主页时,则未设置会话变量。 session() - > all()正在显示空数组。
I am redirecting the page after a successful login check and the session is created but the session is not available after page redirection.
My Login Controller:
$validated = $request->validate([
'email' => 'required|email',
'password' => 'required',
'g-recaptcha-response' =>'required|captcha',
],
[
"g-recaptcha-response.required" => "Captcha is required!",
]);
if(usernamd and password matches){
session()->put("username",$username);
return redirect()->route('home');
}else{
return Redirect::to("/login")->withFail('Wrong Passoword entered.');
}
if I print the session variables, they are visible and have proper values, but when it is redirected to the home page, the session variables are not set.
session()->all() is showing empty array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您放置完整的控制器代码。 Laravel中的会话仅与应用程序\ HTTP \ kernel类中定义的“ Web”中间件一起工作。如果您从API提出请求,则必须使用Laravel/Sanctum或手动从会话外墙开始会议。
I suggest you put the complete controller code. Sessions in Laravel only work with the "web" middleware defined in the App\Http\Kernel class. If you make the request from an API, you must use laravel/sanctum or manually start the sessions with the Session facade.
为了了解您想做什么,您需要提供更多信息。我认为您要做的是在会话中存储一个临时值,以供以后使用。这是一些可以作为指导的代码。您可能知道,编写代码的方法有很多,每个方法都是根据需要编写的。但是,您应该尽可能简化代码。尝试遵循Laravel公约和最佳实践。请阅读在这里。
In order to understand what you want to do, you need to provide more information. I think what you're trying to do is store a temporary value in the session for later use. Here is some code that can serve as a guide for that. As you probably know, there are many ways to write code, each one is written according to the need. However, you should simplify the code as much as possible. Try to follow Laravel conventions and best practices. Please read here.