session怎么在Symfony2中使用

session怎么在Symfony2中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

Symfony自带有session的方法,以前老版本2.2及以前的session用法是

$session = $this->getRequest()->getSession();
$session->set('foo', 'bar');
$foobar = $session->get('foobar');

后来Symfony2.3开始$this->getRequest()方法被废弃,session的使用方法就变成了

use Symfony\Component\HttpFoundation\Request;
public function indexAction(Request $request)
{
  $session = $request->getSession();
  // store an attribute for reuse during a later user request
  $session->set('foo', 'bar');
  // get the attribute set by another controller in another request
  $foobar = $session->get('foobar');
  // use a default value if the attribute doesn't exist
  $filters = $session->get('filters', array());
}

看完上述内容,你们掌握session怎么在Symfony2中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!