【问题标题】:Symfony cookie not sent (but in Response Header)Symfony cookie 未发送(但在响应标头中)
【发布时间】:2017-08-01 02:48:36
【问题描述】:

我不明白为什么我的 cookie 没有发送。 我已经检查了所有关于它的线程,看来我做得对。 我在 Symfony 调试中看不到 cookie,但在我的浏览器上看不到。

    public function indexAction(Request $request)
{


    $response = new Response();
    $userID = $request->cookies->get('myCookie123');

    if (empty($userID)) {

        $uniqueID = $this->generateIDUtil();
        $response->headers->setCookie(new Cookie('myCookie123', $uniqueID, 2147483647));
        $response->sendHeaders();

    }else{
        var_dump('USER ALREADY KNOW <br>' );
    }

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


    return $response->setContent($this->render('MyBundle:Core:index.html.twig'));
}

【问题讨论】:

    标签: php cookies symfony


    【解决方案1】:

    以下是正确创建 cookie 并作为响应发送给客户端的方法:

    $res = new Response();
    $cookie = new Cookie(
            'my_cookie',
            $student->getStuId(),
            time() + ( 2 * 365 * 24 * 60 * 60)  // Expires 2 years.
    );
    $res->headers->setCookie( $cookie );
    $res->send();
    

    我一直在使用它,所以如果您还有问题,请告诉我们。

    【讨论】:

    • 谢谢,终于成功了。我不确定是什么问题。要么是我在 Cookie 类中设置的时间,要么是我的 USE 语句搞砸了。我不明白,因为唯一的区别是持续时间到期,并且您使用的是 ->send 而不是 ->senHeader。任何谢谢!
    • $res->send();导致空白页。我在下面发布了渲染视图并避免空白页的解决方案
    【解决方案2】:

    因此,对于任何想要从 cookie 中获取 userId 的人来说,这里有一段工作代码。带有所有的use语句和twig渲染模板。

    上述解决方案的问题在于,当您发送标头而不渲染视图时,它会给您一个空白页面。

    因为它的记录真的很糟糕,所以这是一个可行的解决方案。

    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Cookie;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    
    class tempController extends Controller
    {
        public function indexAction(Request $request)
        {
    
            $user = $request->cookies->get('myCookie'); // $user is a string (the userID) or null if there is no cookie.
    
            if (!empty($user)) {  
                //Fetch user data in the DB
            }
    
            // some code ... 
    
    
            $response = new Response();
    
            if ($user instanceof User) {
                // do some stuff like pre fill a form
            }else{
                // We don't know the user we send a cookie.
                $cookie = new Cookie('myCookie', $this->generateIDUtil(), time() + (365 * 24 * 60 * 60));  // Expires 1 years
                $response->headers->setCookie($cookie);
                $response->sendHeaders();
            }
    
            //Render the view  WITH the response (It's very important to put $response as 3rd parameter)
            return $this->render('MyBundle:Core:index.html.twig', array(/* view param */), $response);
        }
    }
    

    【讨论】:

    • 你好,Kaizoku。我给出的上述解决方案,是的,只将cookie设置为标题。您仍然需要像往常一样呈现页面。我通常做的是设置 cookie,然后重定向到路由。例如:return $this-&gt;redirectToRoute('homepage'); 紧跟在 $response-&gt;send(); 之后,您甚至可以渲染同一个页面。
    • 非常感谢您的解释!奇迹般有效!一个多星期以来一直在为这个问题苦苦挣扎。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2019-12-24
    • 2019-04-26
    • 2021-05-17
    • 2014-08-24
    • 2012-12-31
    相关资源
    最近更新 更多