Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3918

How to get the current session array count in twig?

$
0
0

In my bootstrap file where I instantiate my twig I have:

$twig->addGlobal('cart', $session->get('cart'));

and in top navbar of my twig I have a badge to show how many items are in added in cart as below:

{{ cart|length }}

and my main file that is called after bootstrap file I said above, I have:

if (!empty($_getvars['id'])) {
    $data = $session->get('cart');
    if(!isset($data[$_getvars['id']])) {
        $data[$_getvars['id']] = 0;
    }
    $data[$_getvars['id']] += 1;
    $session->set('cart', $data);
} 
print_r($session->get('cart'));

adding to sessions is working fine, and print debug above shows that it is accurate, but in the top navbar badge I always get the previous amount of items rather than current amount unless otherwise I refresh the page to show the current. How to fix it?


Viewing all articles
Browse latest Browse all 3918

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>