Cómo usar las sesiones en Symfony? [cerrado]


Al igual que en PHP clásico, usamos las variables mágicas para iniciar y crear sesiones, así que ¿cómo hacerlo en Symfony?

Author: Robert Wade, 2009-11-19

2 answers

En su controlador, puede acceder a las variables de sesión a través del objeto user.

// Get a session value
$name = $this->getUser()->getAttribute('name', 'default_value');
// Set a session value
$this->getUser()->setAttribute('name', $value);
 32
Author: Franz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-05 03:26:59

En Symfony2, la sintaxis es diferente:

$session = $this->getRequest()->getSession();

// store an attribute for reuse during a later user request
$session->set('foo', 'bar');

// in another controller for another request
$foo = $session->get('foo');

También puede obtener variables de sesión de Twig, sin tener que pasar la variable de sesión explícitamente (está en la 'aplicación'global):

{{ app.session.get('foo', 'bar'); }}
 80
Author: Tac Tacelosky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-14 12:46:08