Cookie basics for Symfony, Sensio Labs PHP framework

In Symfony, here is how you “get cookies”:

Don’t forget to include the class path:

Setting cookie values in Symfony is pretty straight forward, the thing to know is that since Symfony processes so much in its front controllers you can set cookies deeper into your code than other methods where headers are sent much sooner. Since cookies can only be set before headers, this allows more flexibility within your design and coding and can also create problems like “where did that cookie go?!” unlike the olden days when you just assumed it was at the start of the new script!

Accessing cookies directly in your TWIG template:
By the way, a separate templating engine for your views that has built-in log and ability to iterate values, etc is awesome, if you haven’t used TWIG, I highly recommend it!!!

Here’s how to clear a cookie:

In controller to update cookies, don’t forget to “SEND” the response.

Aaron Belchamber has written 243 articles

Leave a Reply

Your email address will not be published. Required fields are marked *