如何创建一个将在Symfony2中过期/销毁会话的命令控制台?

时间:2014-01-07 09:14:05

标签: session symfony destroy

伙计们我想创建一个命令控制台,它会使会话过期或破坏Symfony 2中的会话?这是我的初始代码,它不起作用:(谢谢希望你能帮助我

public function configure()
    {
        $this
            ->setName('pat:session:delete')
            ->setDescription('Delete a existing session')
            ->addArgument('session_id', InputArgument::REQUIRED, 'The ID of session that will destroy')
        ;
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $sessionId = $input->getArgument('session_id');

        $sessionQuery = SessionQuery::create()
            ->filterById($sessionId)->findOne()->toArray();

        $session = $this->getContainer()->get('session');

        //Option 1
        $session->migrate($destroy = false, $lifetime = null);

        //Option 2
        $session->invalidate();

        //Option 3 
        $session->getMetadataBag()->stampNew(1);

        //Option 4
        $session->remove($sessionQuery["Id"]);

    }

1 个答案:

答案 0 :(得分:0)

//通过控制器(Symfony 2.6+)注销

$这 - >获得( 'security.token_storage') - > setToken(空); $这 - >获得( '请求') - >的getSession() - >无效();

more info...