我在 Laravel 5.4 中有这个函数,但是无法从 cookie 中获取任何内容。
Cookie::queue('currentLang', 'heb', 999999999);
echo $request->cookie('currentLang');
但是我得到了一些长字符串而不是我设置的内容。
我在 Laravel 5.4 中有这个函数,但是无法从 cookie 中获取任何内容。
Cookie::queue('currentLang', 'heb', 999999999);
echo $request->cookie('currentLang');
Laravel有一个cookie助手!
/**
* Create a new cookie instance.
*
* @param string $name
* @param string $value
* @param int $minutes
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
* @return \Symfony\Component\HttpFoundation\Cookie
*/
function cookie($name = null, $value = null, $minutes = 0, $path = null, $domain = null, $secure = false, $httpOnly = true)
{
$cookie = app(CookieFactory::class);
if (is_null($name)) {
return $cookie;
}
return $cookie->make($name, $value, $minutes, $path, $domain, $secure, $httpOnly);
}
然后使用:
Cookie::get('$name')
use Illuminate\Support\Facades\Cookie;
Cookie::queue('currentLang', 'heb', 999999999);
echo Cookie::get('currentLang');
我也遇到了相同的问题。它返回 null,所以我用 Javascript 和 PHP 做了这件事,像这样:
使用 Javascript 在 cookie 中设置值
function createCookie(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
使用PHP检索cookie
$_COOKIE["popupId"]