簡體   English   中英

Laravel session 用戶登錄后數據為空

[英]Laravel session data empty after user login

我正在使用 laravel 版本 6.17.0。

If I put data to session Session::put('cart_merge', $data) in authenticated() method of AuthenticatesUsers trait and I try to get them with Session::get('cart_merge') in Controller, the session is empty . 如果我在插入authenticated()方法后立即執行Session::get('cart_merge') ,它們仍然存在。

我嘗試按照https://stackoverflow.com/a/35140419/5821265中的建議對路線進行分組,但沒有幫助。

為什么 session 直到 Controller 才成功?

AuthenticatesUsers.php:

protected function authenticated(Request $request, $user)
{
    $cart = getCart::getCart();
    $savedCart = $user->cart;

    if (empty($cart['cart'])){  // Shopping cart is empty
        $cart = [];
        foreach ($savedCart as $item) {
            $product = Product::where('id', $item->id)->where('visibility', 1)->where('stock', '>', 0)->first();
            if (is_null($product)) {
                continue;
            }
            if ($item->pivot->amount > $product->stock){
                $cart[$item->id]['amount'] = $product->stock;
            }
            else {
                $cart[$item->id]['amount'] = $item->pivot->amount;
            }
            $cart[$item->id]['name'] = $product->name;
            $cart[$item->id]['url'] = $product->url;
            $cart[$item->id]['image'] = $product->image;
        }
        Session::put('cart', $cart);
    }
    else {  // There are some items in shopping cart.
        if ($savedCart->isEmpty()){     // User doesn't have any items saved in DB from last session.
            foreach ($cart['cart'] as $item_id => $item){
                DB::table('product_user')->insert([
                    'user_id' => $user->id,
                    'product_id' => $item_id,
                    'amount' => $cart['cart'][$item_id]['amount'],
                    'type' => 0
                ]);
            }
        }
        else {  // User has saved items in DB from last session.
            Session::put('cart_merge', $savedCart->toArray());
            // if I do Session::get('cart_merge') here, the data are correct.
        }
    }
}

SystemController.php:

class SystemController extends Controller
{
    public function index(){
        $data = Session:get('cart_merge');
        dd($data);
        // if I do Session::get('cart_merge') here, null is returned instead of data.
    }
}

web.php:

Route::group(['middleware' => ['web']], function () {
    Route::get('/', 'SystemController@index');
    Auth::routes();
});

您正在直接在 class 中訪問 session

Session 取決於請求。

因此,您的請求方法可能會路由到某些 controller 方法,然后您可以訪問 session。 否則 laravel 將如何知道它是誰的 session

前任

Route::group(['middleware' => 'web'], function () {
    Route::get('', function () {
        Session::set('test', 'testing');
    });

    Route::get('other', function () {
        dd(Session::get('test'));
    });
});

https://stackoverflow.com/a/35140419/4153682檢查這個

通過將 session 驅動程序更改為SESSION_DRIVER=file而不是cookies文件中的 cookies 來.env 似乎 cookies 選項不是持久的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM