symfony - sfUser 等价于 Symfony 2

标签 symfony

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/

相关文章:

json - 在 Symfony3 中捕获登录失败事件

php - 如何从普通 SQL 中的 NOT EXISTS 转换为 DOCTRINE ORM

php - Symfony2 实体集合 - 如何添加/删除与现有实体的关联?

php - 如何停止 Doctrine2 迁移 :Diff from always adding foreign key relationships that already exist in Database?

validation - Symfony2 : entity asserts that accept an array of types?

events - Symfony2 : PrePersist/PreUpdate lifecycle-event not fired

php - symfony 错误 : Expected =, <, <=, <>, >, >=, !=, 得到 '.'

php - 在 Symfony2 中使用 Doctrine 和静态 YAML 数据库

php - 创建用户,但使用 PHP 检查 Active Directory 中是否存在

php - Symfony2 功能测试 session 持久性