Home  >  Article  >  Backend Development  >  Symfony2 session usage example analysis

Symfony2 session usage example analysis

WBOY
WBOYOriginal
2016-07-29 09:04:05997browse

This article analyzes the usage of Symfony2 session with examples. Share it with everyone for your reference, the details are as follows:

Symfony has its own session method. The session usage in the old version 2.2 and before was

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

Later, starting from Symfony2.3, the $this->getRequest() method was Abandoned, the usage of session becomes

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());
}

The permanent address of this article: http://blog.it985.com/13586.html
This article is from IT985 blog. Please indicate the source and corresponding link when reprinting.

Readers who are interested in more content related to PHP framework can check out the special topics of this site: "Summary of Excellent PHP Development Framework", "Introduction Tutorial on Codeigniter", "Advanced Tutorial on CI (CodeIgniter) Framework", "Introduction and Common Use of Yii Framework" Summary of Skills" and "Introduction to ThinkPHP Tutorial"

I hope this article will be helpful to everyone's PHP programming based on the Symfony framework.

The above introduces the example analysis of Symfony2 session usage, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn