在Laravel 5中间件中获取Cookie [英] Get a cookie in Laravel 5 middleware

查看:46
本文介绍了在Laravel 5中间件中获取Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Laravel 5.3中的中间件检索cookie,但似乎$ request-> cookie('language')为空.我猜它只有在中间件运行之后才能设置.

I'm trying to retrieve a cookie from a middleware in Laravel 5.3 but it seems like $request->cookie('language') is empty. I'm guessing that it is only set after the middleware runs.

我读到某个地方应该使用\ Cookie :: queued('language'),但它仍然是空的.

I read somewhere that I should use \Cookie::queued('language'), but it's still empty.

我唯一的选择是使用$ _COOKIE变量吗?

Is my only option using the $_COOKIE variable?

推荐答案

您何时设置此Cookie?

When do you set this cookie?

请记住cookie是存储在浏览器中的,因此用户需要获取响应才能使您以后能够检索cookie.

Remember that cookies are stored in the browser, so the user needs to get the response in order for you to be able to retrieve the cookie later.

通过成功发送给用户的响应设置cookie后,您应该能够获取cookie.还要记住,如果使用dd(),则不会创建cookie,因为它会跳过所有cookie头都不会发送给用户.

You should be able to get the cookie after the cookie is being set by a response that's successfully sent to the user. Remember also that if you use dd(), that doesn't let the cookie get created, because it skips all cookie headers from being sent to the user.

尝试从中间件获取cookie时可能遇到的另一个问题是,它可能不会自动解密,因此您必须自己做.

Another problem you might face for trying to get cookies from middleware is that it might not get decrypted automatically, so you'll have to do it yourself.

示例:

\Crypt::decrypt(Cookie::get('language'))

这篇关于在Laravel 5中间件中获取Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆