简体   繁体   English

如何在 Laravel 5.4 中设置 cookie?

[英]How to set cookie in laravel 5.4?

I have this function in laravel 5.4 but I cant get anything from cookie.我在 laravel 5.4 中有这个功能,但我无法从 cookie 中得到任何东西。

Cookie::queue('currentLang', 'heb', 999999999);

echo  $request->cookie('currentLang');

But I am getting some long string and not what I set.但是我得到了一些长字符串而不是我设置的字符串。

use Illuminate\Support\Facades\Cookie;

Cookie::queue('currentLang', 'heb', 999999999);

echo Cookie::get('currentLang');

Laravel has a cookie helper! 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);
}

then use: 然后使用:

Cookie::get('$name')

I also face to the same issue.我也面临同样的问题。 it returns null so I have done it using Javascript and PHP like它返回 null 所以我已经使用 Javascript 和 PHP 完成了它

Setting value in cookie using Javascript使用 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=/";
    }

Retriving cookie using PHP使用 PHP 检索 cookie

$_COOKIE["popupId"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM