簡體   English   中英

Symfony2:讀取Cookie

[英]Symfony2 : Read Cookie

我在Controller動作中設置了一些cookie,然后在另一個動作中我想要讀取cookie集並對值進行一些操作。 但是,在嘗試讀取cookie時,我看到的只是一個空數組,我的代碼如下:

public function testSetCookieAction()
{
    $value = 'ABCDEFGHI'

    $cookie = new Cookie('SYMFONY2_TEST', $value, (time() + 3600 * 24 * 7), '/');
    $response = new Response();
    $response->headers->setCookie($cookie);
    $response->send();  
.
.
.
}

public function testReadCookieAction()
{
    $response = new Response();
$cookies = $response->headers->getCookies();

// $cookies = array(0) { } 
}

當我var_dump($_COOKIE); ,我看到array(1) { ["SYMFONY2_TEST"]=> string(9) "ABCDEFGHI" }有人知道我做錯了什么嗎?

提前致謝

您必須在Request對象上讀取cookie,而不是在剛創建的void Response對象上讀取;)

public function testReadCookieAction(Request $request)
{
    $cookies = $request->cookies;

    if ($cookies->has('SYMFONY2_TEST'))
    {
        var_dump($cookies->get('SYMFONY2_TEST'));
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM