Kaizoku Gambare
Kaizoku Gambare

Reputation: 3413

Symfony cookie not sent (but in Response Header)

I can't understand why my cookie isn't sent. I've check all the thread about it and it seems that I'm doing it right. I can't see the cookies in the Symfony debug, but not on my browser.

    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'));
}

enter image description here

Upvotes: 3

Views: 5945

Answers (2)

Kaizoku Gambare
Kaizoku Gambare

Reputation: 3413

So here a working piece of code for anyone that want to get a userId from a cookie. With all the use statement and the twig rendering template.

The problem with the solution above is that it will give you a blank page as you send the header without rendering the view.

As it's really badly documented, here is a working solution.

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);
    }
}

Upvotes: 6

Alvin Bunk
Alvin Bunk

Reputation: 7764

Here is how you properly create a cookie and send as a Response to a client:

$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();

I use it all the time, so please let us know if you still have problems.

Upvotes: 2

Related Questions