【问题标题】:Symfony2: Reading a Cookie with Request object not workingSymfony2:读取带有请求对象的 Cookie 不起作用
【发布时间】:2014-09-23 06:09:54
【问题描述】:

当我创建一个 cookie 时,$response 对象可以完美地读取它,但是当页面刷新时我得到一个空数组。在google上搜索示例,发现必须使用请求对象来完成,但它根本不起作用。你知道我缺少什么吗?

public function indexAction(Request $request) {

    $response = new Response();

    var_dump($response->headers->getCookies()); //GIVES EMPTY ARRAY
    var_dump($request->cookies->all()); //GIVES ONLY PHPSESSID, BUT NOT THE ONE CREATED
    var_dump($request->cookies->get('user')); //GIVES NULL

    // ... //

    if ($loginForm->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $dql = // ... //                    ));
        $result = $dql->getArrayResult();

        if ($result) {
            foreach ($result as $profile) {
                $cookie = // ... //
                $response->headers->setCookie(new Cookie('user', $cookie, $expire = 0, $path = '/', $domain = 'null', $secure = false, $httpOnly = false));
                $response->sendHeaders();
            }

            var_dump($response->headers->getCookies()); //GIVES CORRECT COOKIE OBJECT
            var_dump($request->cookies->all()); //GIVES ONLY PHPSESSID, BUT NOT THE ONE CREATED
            var_dump($request->cookies->get('user')); //GIVES NULL
        }

【问题讨论】:

    标签: php symfony cookies request response


    【解决方案1】:

    您将域设置为“null”(一个字符串),因此当您尝试读取它时,它不会显示,因为您不在“null”域下。如果你想使用默认值,它应该只是 null(不是字符串)。

    【讨论】:

      猜你喜欢
      • 2012-01-15
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多