I’m using Silex, and I’m wondering if it is possible to use an associative array for a flash session variable? I can’t seem to get it to work.
Here’s what I’ve been trying:
Setting up the flash var in my app file.
$my_array = array('fruit' => 'apple', 'veggie' => 'carrot');
$app['session']->setFlash('myFlashVar', $my_array);
And used in Twig like this.
{% set myVar = app.session.getFlash('myFlashVar') %}
// And so that we don't get an error, check to see if the key exists
{% if 'fruit' in myVar|keys %}{{ myVar['fruit'] }}{% endif %}
Thanks in advance for any help offered.
setFlashandgetFlasha deprecated methods and therefore should be avoided.You can get your code to run by using the favored methods provided by Session::getFlashBag (pun intended?)
PHP:
TWIG:
Disclaimer: I only tested this using the Symfony2 (2.16) framework – not Silex. In Symfony2 it is: