How to validate cookie in Symfony2?

I am trying to use the twig annotation in my view to determine if a certain cookie value has been set, but I am not very familiar with the branch and currently does not work.

Firstly, I set the cookie in the controller and I see that it is present in my browser. Here is the relevant PHP code:

if (isset($_GET['accept-cookie'])) {
    setcookie("acceptCookies", "true", time() + (86400 * 300));
}

In my browser, I can view the cookie and confirm that the content is correctly set to true.

Now, in my opinion, I need to check if a cookie is present, if this does not mean a cookie banner should be displayed. Here is the code:

{% if app.request.cookies.get("acceptCookies") != null %}
    <h1>Cookies are set!</h1>
{% else %}
    <div id="cookieBanner">
        <div id="cookieContainer">
            <p>We use cookies on this website. By using this website, we'll assume that you consent to <a href="/cookies">the cookies we set.</a></p>
            <a href="?accept-cookie" class="button">Okay, continue.</a>
        </div>
    </div>
{% endif %}

According to the documentation I read, it should work correctly, however it continues to display a cookie banner, although I see that the browser is present in my browser.

?

+4
2

, cookie .

cookie

{% app.request.cookies %}

:

{% if app.request.cookies.has('acceptCookies') %} Yes you accept cookies {% endif %}

. : http://enarion.net/news/2012/access-cookies-in-a-twig-template-using-symfony2/

symfony , cookie: http://symfony.com/doc/current/components/http_foundation/introduction.html#setting-cookies

+6

php $_GET['accept_cookie'], html twig. cookie. , $_GET['accept_cookie'].

0

All Articles