Symfony에서 세션을 사용하는 방법? [닫은
-
21-09-2019 - |
문제
Classic PHP에서와 마찬가지로 Magic 변수를 사용하여 세션을 시작하고 생성하는 방법은 Symfony에서 어떻게해야합니까?
해결책
컨트롤러에서 사용자 객체를 통해 세션 변수에 액세스 할 수 있습니다.
// Get a session value
$name = $this->getUser()->getAttribute('name', 'default_value');
// Set a session value
$this->getUser()->setAttribute('name', $value);
다른 팁
Symfony2에서 구문은 다릅니다.
$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');
세션 변수를 명시 적으로 전달하지 않고도 Twig에서 세션 변수를 얻을 수도 있습니다 (글로벌 '앱'에 있습니다).
{{ app.session.get('foo', 'bar'); }}
제휴하지 않습니다 StackOverflow