当前位置:首页 > CMS教程 > 其它CMS > 列表

Symfony2 session用法实例分析

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-09 10:34:19 浏览: 评论:0 

这篇文章主要介绍了Symfony2 session用法,结合实例形式分析了Symfony自带session方法的相关使用技巧,需要的朋友可以参考下。

本文实例分析了Symfony2 session用法,分享给大家供大家参考,具体如下:

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

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

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

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

Tags: Symfony2 session

分享到: