Laravel 控制器无法获取 cookie

Fur*_*rya 6 php cookies laravel

我对 Laravel 5.6 还很陌生,我想在控制器中使用 cookie,但我无法检索 cookie。

我的目的是在 cookie 中保存一些数据,而不是将它们放在 url 上,因为我不想要类似的东西:

www.test.com/case/10156/folder/562/client/896/

但更像是

www.test.com/case/folder/client

所以当我点击链接时我的功能:

$(document).on('click', '.link', function(e){
    $.cookie("affaire", $(this).data("affaire"));
    $(location).attr('href',$(this).data("href"));
}); 
Run Code Online (Sandbox Code Playgroud)

它运行良好,在 chrome 上我可以看到 cookie“affaire”。我重定向到 /lots 所以我的路线是:

    Route::get('/lots', ['as' => 'lots', 'uses' => 'Copro\LotController@index']);
Run Code Online (Sandbox Code Playgroud)

还有我的 LotController@index :

    public function index()
    {
        $case_id = Cookie::get('affaire');
        dd($case_id);
    }
Run Code Online (Sandbox Code Playgroud)

但是它返回“null”(我在控制器的开头添加了“use Cookie”)所以我不能使用这个值来做其他事情。

我试过 Request :

public function index(Request $request)
{
    $copro_id = $request->cookie('affaire');
    dd($copro_id);
}
Run Code Online (Sandbox Code Playgroud)

但它也不起作用。

有人知道如何检索cookie吗?

感谢您的帮助。

Fur*_*rya 7

好的,我终于找到了解决我的问题的方法:

https://pineco.de/accessing-front-end-cookies-laravel/

cookie 是用 laravel 加密的,所以我们必须在中间件列表之外输入 cookie 名称。