Symfony 2 中的 sfUser
等效项是什么?
sfUser
允许使用 getAttribute()
方法获取用户 session 属性。
Symfony\Component\Security\Core\User\User
是等价的吗?但它没有 getAttribute()
方法。
最佳答案
要获取 session 属性,请从容器中获取 session 服务。此示例位于 Controller 中,其中 session 可通过辅助方法使用:
public function fooAction()
{
$session = $this->getRequest()->getSession();
// Setting
$session->set("foo", "bar");
// Retrieving
$foo = $session->get("foo");
}
请参阅documentation了解详情。如果需要,您还可以通过 $container->get("session"); 显式从容器中检索 session ;
如果您需要 User 对象,可以通过以下方式获取:
public function fooAction()
{
// Get user from security token (assumes logged in/token present)
$user = $this->get("security.context")->getToken()->getUser();
}
再次参见 documentation了解更多详情。
关于symfony - sfUser 等价于 Symfony 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8358047/