如何检查 laravel 中是否设置了 cookie?
how to check if a cookie is set in laravel?
我用我的控制器制作了一个 cookie,这似乎有效,因为如果我在我的开发人员工具中检查我的资源,它就在那里。但是现在我想在我的视图中对其进行操作,但这似乎不起作用,这是我在我的视图中使用的代码:
@if (Cookie::get('cookiename') !== false)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif
这总是返回 'true'
谁能帮帮我?
改变
@if (Cookie::get('cookiename') !== false)
到
@if (Cookie::get('cookiename') !== null)
null
,未设置cookie时返回false
:https://github.com/illuminate/http/blob/master/Request.php#L363
如果您查看 Cookie class,现在您可以使用 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)
我用我的控制器制作了一个 cookie,这似乎有效,因为如果我在我的开发人员工具中检查我的资源,它就在那里。但是现在我想在我的视图中对其进行操作,但这似乎不起作用,这是我在我的视图中使用的代码:
@if (Cookie::get('cookiename') !== false)
<p>cookie is set</p>
@else
<p>cookie isn't set</p>
@endif
这总是返回 'true'
谁能帮帮我?
改变
@if (Cookie::get('cookiename') !== false)
到
@if (Cookie::get('cookiename') !== null)
null
,未设置cookie时返回false
:https://github.com/illuminate/http/blob/master/Request.php#L363
如果您查看 Cookie class,现在您可以使用 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)