简体   繁体   English

如何检查是否在laravel中设置了cookie?

[英]how to check if a cookie is set in laravel?

I have made a cookie with my controller and this seems to work because if I check my resources in my developer tools it is there. 我用我的控制器制作了一个cookie,这看起来很有效,因为如果我在开发人员工具中检查我的资源就可以了。 But now I want to do actions with it in my view , but this doesn't seem to work , this is the code I used in my view: 但是现在我想在我的视图中用它做一些动作,但这似乎不起作用,这是我在视图中使用的代码:

@if (Cookie::get('cookiename') !== false)
    <p>cookie is set</p>
@else
    <p>cookie isn't set</p>
@endif

this is always returning 'true' 这总是回归'真'

Can anyone help me ? 谁能帮我 ?

change 更改

@if (Cookie::get('cookiename') !== false)

to

@if (Cookie::get('cookiename') !== null)

null , not false is returned when cookie isn't set: https://github.com/illuminate/http/blob/master/Request.php#L363 null未设置cookie时返回falsehttps//github.com/illuminate/http/blob/master/Request.php#L363

If you check out the Cookie class now you can use Cookie::has('cookieName'); 如果你现在检查Cookie类,你可以使用Cookie::has('cookieName');

class Cookie extends Facade
{
    /**
     * Determine if a cookie exists on the request.
     *
     * @param  string  $key
     * @return bool
     */
    public static function has($key)
    {
        return ! is_null(static::$app['request']->cookie($key, null));
    }

    // ...

你可以用它

if($request->hasCookie('cookie_name') != false)

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

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