dsjpik057730 2018-07-06 08:58
浏览 85

如何使用symfony 4使Controller无效

My application is being migrated from symfony 3 to symfony 4, I dont know whether this is a symfony 4 bug or not.

Im using a rest api that uses a controller which creates and removes a session.

when I create a session and set a variable.

$session = new Session(); // or $this->get("session") 
$session->start();
$session->set("key", "value");

and try to invalidate the sesssion. (Ive tried all of these)

$session = new Session();
$session->invalidate(1);
$session->clear();

$this->get("session")->invalidate(1);

$request->getSession()->invalidate(1);

the value can still get accessed with the get method. e.g with

$request->getSession()->get("key");

Also the services to set the token to null and not available anymore.

  • 写回答

1条回答 默认 最新

  • dougouqin0763 2018-07-08 15:10
    关注

    First get the currently existing session

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

    To set a variable:

    $session->set('key', 'value');
    

    To clear all variables:

    $session->clear();
    

    To invalidate the session

    $session->invalidate();
    

    When I then do

    var_dump($request->getSession()->get("key"));
    

    it returns null

    I guess the problem is that you set $session twice

    $session = new Session();
    
    $session->set('key', 'value');
    
    $session = new Session();
    
    $session->invalidate(1);
    
    var_dump($request->getSession()->get("key"));
    

    This returns string 'value' while

    $session = new Session();
    
    $session->set('key', 'value');
    
    $session->invalidate(1);
    
    var_dump($request->getSession()->get("key"));
    

    returns null

    评论

报告相同问题?