symfony2升级2.0> 2.3 Symfony \ Component \ HttpFoundation \ Session的问题

时间:2014-12-05 20:37:49

标签: php symfony session upgrade symfony-http-foundation

这是我的自定义安全监听器(来自sym 2.0)

namespace mine\UserBundle\EventListener;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Session as Session;


class SecurityListener
{
    protected $security;
    protected $session;

/**
* Constructs a new instance of SecurityListener.
*
* @param SecurityContext $security The security context
* @param Session $session The session
*/
    public function __construct(SecurityContext $security, Session $session)//<-- here
    {
        //You can bring whatever you need here, but for a start this should be useful to you
        $this->security = $security;
        $this->session = $session;
    }

/**
* Invoked after a successful login.
*
* @param InteractiveLoginEvent $event The event
*/
    public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
    {
         //Your logic needs to go here
         //You can addRole 
         //Even persist if you want but bring the right tools to your constructor
         $security = $this->security; 

         if ($security->getToken()->getUser()->hasPlus()) {       
            $security->getToken()->getUser()->addRole('ROLE_PLUSUSER');    
         }

    }
}

我假设某些命名空间或类似的在2.0和2.1之间发生了变化(我正试图将它一直提升到2.3)因为我抛出了这个错误:

ContextErrorException:Catchable Fatal Error:传递给我的参数2 \ UserBundle \ EventListener \ SecurityListener :: __ construct()必须是Symfony \ Component \ HttpFoundation \ Session的实例,Symfony \ Component \ HttpFoundation \ Session \ Session的实例给出

1 个答案:

答案 0 :(得分:1)

好的,将依赖项更改为

使用Symfony \ Component \ HttpFoundation \ Session \ Session;

做了这个伎俩。与错误信息所说的完全相反......