How to set session variables for all the controllers in Symfony2?

后端 未结 3 904
不思量自难忘°
不思量自难忘° 2021-02-13 04:48

How do I create and access Symfony 2 session variables in my controllers. I used like this.

$session = new Session();
$session->start();
$session->set(\'lo         


        
3条回答
  •  孤独总比滥情好
    2021-02-13 05:38

    http://symfony.com/doc/current/components/http_foundation/sessions.html

      use Symfony\Component\HttpFoundation\Session\Session;
    
      $session = new Session();
      $session->start();
    
      // set and get session attributes
      $session->set('name', 'Drak');
      $session->get('name');
    
      // set flash messages
      $session->getFlashBag()->add('notice', 'Profile updated');
    
      // retrieve messages
         foreach ($session->getFlashBag()->get('notice', array()) as $message) {
         echo '
    '.$message.'
    '; }

提交回复
热议问题