Laravel,Guzzle - 检查Cookie是否已设置

时间:2017-05-24 02:52:33

标签: php laravel cookies

我希望在底部getUsername()时检查Cookie是否已设置。任何人都可以帮我快速解决这个问题吗?我没试过好几个小时。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cookie;

class CookieController extends Controller 
{
    public function setCookie(\stdClass $request)
    {
        $minutes = 60;
        $response = new Response(view('panel.panel'));
        $response->withCookie(cookie('userInfo', $request, $minutes));
        return $response;
    }
    public function getCookie(){
        $val = cookie::get('userInfo');
        return $val;
    }

    public function getUsername(){
        $cookie = cookie::get('userInfo');
        return $cookie->message->username;
    }

    public function getShopID(){
        $cookie = cookie::get('ShopID');
        return $cookie->message->shopID;
    }
}
?>

2 个答案:

答案 0 :(得分:1)

您可以使用operator()()或与isset

进行比较
null

答案 1 :(得分:0)

尝试使用方法hasCookie()

public function getUsername(Request $request)
{
    if ($request->hasCookie('userInfo')) {
        $cookie = $request->cookie('userInfo');
        return $cookie->message->username;
    }
}