交响乐 2.5 : $this->getUser() is empty

标签 symfony

  1. 我有一个用户可以登录的表单。
  2. 登录后,我会将用户重定向到仪表板页面。

问题:在我的登录 Controller 中,我得到了用户对象。但是当我转到仪表板时,用户对象是空的。

登录 Controller

 if ($this->isAuthUser($request)) {
          $signinUser = $request->get('signin');
          $token = new UsernamePasswordToken($signinUser, null, 'secured_area', $signinUser->getRoles());
          $this->get("security.context")->setToken($token);      

          $event    = new InteractiveLoginEvent($request, $token);
          $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

          $session = $this->get('session');
          $session->set('user', $signinUser->getUsername());
    }

安全.yml

security:
    providers:        
        in_memory:
            memory: ~

    access_control: 
      #- { path: ^/(signin|signup)?$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
      #- { path: ^/, roles: IS_AUTHENTICATED_FULLY }      

    firewalls:
      secured_area:
        pattern:   ^/
        anonymous: true
        form_login:
          login_path: /signin
          check_path: _security_check
        access_denied_url: signin
#        logout:
#          path:   /signout
#          #target: /
#          invalidate_session: false
#          delete_cookies:
#              a: { path: null, domain: null }
#              b: { path: null, domain: null }
#          #handlers: [some.service.id, another.service.id]
#          #success_handler: some.service.id
      dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

      default:
        anonymous: true

token 对象:Signincontroller

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
    [credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => 
    [providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
    [user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Headset\Bundle\AccountsBundle\Entity\User Object
        (
            [email:Headset\Bundle\AccountsBundle\Entity\User:private] => operator1@flavr.com
            [password:Headset\Bundle\AccountsBundle\Entity\User:private] => operator123#46
            [oldPassword:Headset\Bundle\AccountsBundle\Entity\User:private] => 
            [id:Headset\Bundle\AccountsBundle\Entity\User:private] => 
            [userId:Headset\Bundle\AccountsBundle\Entity\User:private] => 
            [firstName:Headset\Bundle\AccountsBundle\Entity\User:private] => 
            [lastName:Headset\Bundle\AccountsBundle\Entity\User:private] => 
            [username:Headset\Bundle\AccountsBundle\Entity\User:private] => operator1@flavr.com
        )

    [roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
            [0] => Symfony\Component\Security\Core\Role\Role Object
                (
                    [role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
                )

        )

    [authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
    [attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
        )

)

仪表板 Controller

Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken Object
(
    [credentials:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => 
    [providerKey:Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken:private] => secured_area
    [user:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 

    [roles:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
            [0] => Symfony\Component\Security\Core\Role\Role Object
                (
                    [role:Symfony\Component\Security\Core\Role\Role:private] => ROLE_USER
                )

        )

    [authenticated:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => 1
    [attributes:Symfony\Component\Security\Core\Authentication\Token\AbstractToken:private] => Array
        (
        )

)

请问有谁知道是什么原因吗?

任何帮助将不胜感激......

最佳答案

可能问题出在配置上。根据配置的用户提供者部分,您根本就没有任何用户。

security:
    providers:        
        in_memory:
            memory: ~

查看关于 users loading 的 symfony 文档

关于交响乐 2.5 : $this->getUser() is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354920/

相关文章:

交响乐团 : Overriding Twig's routing path function

php - Symfony - Guzzle 请求 : cURL error 6: Could not resolve host

php - 如何在 onFlush 事件监听器中更新实体的 manyToMany 集合?

php - 在symfony2中引用静态模板

带有可选参数的注释中的 Symfony2 路由

node.js - Assetic、twitter bootstrap 等

symfony - 如何在 Symfony2/Twig 中将 form_rest() 渲染为隐藏字段?

php - Symfony 4 文件 Controller 助手如何将二进制文件转换为兼容的 HTML?

php - 传递给 Symfony\Component\Form\FormRenderer::renderBlock() 的参数 1 必须是 ...\FormView 的实例,给定 ...\Form 的实例

php - Symfony 如何删除文件