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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30199736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:32:06  来源:igfitidea点击:

how to check if a cookie is set in laravel?

phplaravelcookies

提问by Jim Peeters

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. 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:

我用我的控制器制作了一个 cookie,这似乎有效,因为如果我在我的开发人员工具中检查我的资源,它就在那里。但是现在我想在我的视图中对其进行操作,但这似乎不起作用,这是我在视图中使用的代码:

@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 ?

谁能帮我 ?

回答by Limon Monte

change

改变

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

to

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

null, not falseis returned when cookie isn't set: https://github.com/illuminate/http/blob/master/Request.php#L363

null, 未false设置 cookie 时不返回:https://github.com/illuminate/http/blob/master/Request.php#L363

回答by mtpultz

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));
    }

    // ...

回答by hamed

you can use this

你可以用这个

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