在Laravel应用程序中的页面重定向后,会话不会被保存

发布于 2025-01-27 08:49:41 字数 610 浏览 4 评论 0原文

成功登录检查并创建会话后,我将重定向页面,但是页面重定向后不可用。

我的登录控制器:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

思念绕指尖 2025-02-03 08:49:41

我建议您放置完整的控制器代码。 Laravel中的会话仅与应用程序\ HTTP \ kernel类中定义的“ Web”中间件一起工作。如果您从API提出请求,则必须使用Laravel/Sanctum或手动从会话外墙开始会议。

if (!Session::isStarted()) {
    Session::start();
}

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.

if (!Session::isStarted()) {
    Session::start();
}
樱花细雨 2025-02-03 08:49:41

为了了解您想做什么,您需要提供更多信息。我认为您要做的是在会话中存储一个临时值,以供以后使用。这是一些可以作为指导的代码。您可能知道,编写代码的方法有很多,每个方法都是根据需要编写的。但是,您应该尽可能简化代码。尝试遵循Laravel公约和最佳实践。请阅读在这里


<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class SomeController {
    public function store(Request $request)
    {
        $request->validate([
            'email'                => 'required|email',
            'password'             => 'required',
            'g-recaptcha-response' => 'required|captcha',
        ], [
            "g-recaptcha-response.required" => "Captcha is required!",
        ]);

        $user = User::whereEmail($request->email)->first();

        if (!$user || !Hash::check($request->password, $user->password)) {
            throw ValidationException::withMessages([
                'email' => [trans('auth.failed')],
            ]);
        }

        $request->session()->put(['login.username' => $user->username]);

        return to_route('home');
    }
}

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.


<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class SomeController {
    public function store(Request $request)
    {
        $request->validate([
            'email'                => 'required|email',
            'password'             => 'required',
            'g-recaptcha-response' => 'required|captcha',
        ], [
            "g-recaptcha-response.required" => "Captcha is required!",
        ]);

        $user = User::whereEmail($request->email)->first();

        if (!$user || !Hash::check($request->password, $user->password)) {
            throw ValidationException::withMessages([
                'email' => [trans('auth.failed')],
            ]);
        }

        $request->session()->put(['login.username' => $user->username]);

        return to_route('home');
    }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文