开发者

How to use Sessions in Symfony? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the hel开发者_运维技巧p center. Closed 10 years ago.

Like in classic PHP we use the magic variables to start and create sessions, so how to do that in Symfony?


In Symfony2, the syntax is different:

$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');

You can also get session variables from Twig, without having to pass the session variable explicitly (it's in the global 'app'):

{{ app.session.get('foo', 'bar'); }}


In your controller, you can access session variables through the user object.

// Get a session value
$name = $this->getUser()->getAttribute('name', 'default_value');
// Set a session value
$this->getUser()->setAttribute('name', $value);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜