1

I have a question. I have a route on my site where I put in session an variable like this:

 public function userCaptcha(){
    $_SESSION['isFacebookRegistration'] = 0;
 }

Now I have another route witch render a view :

public function index(){
    $this->session = $_SESSION;
    return $this->render('template/index.twig');
}

In the index template I do :

 {{ dump(session.isFacebookRegistration) }}
 {% set session = session|merge({'isFacebookRegistration' : 3}) %}

I access the first route : userCaptcha() one time but the route index() 2 times, normally I need to see the first time 0 and the second 3. But I see only the 0 for 2 times. Can you help me please? The idea is to show for first time 0 for the rest 3. Thx in advance

TanGio
  • 766
  • 2
  • 12
  • 34

1 Answers1

0

You cannot set a PHP var on Twig side. Every time your view is reloaded, changes you made to your variable will be lost. You can try something like this:

public function userCaptcha(){
    $_SESSION['isFacebookRegistration'] = 0;
}

public function index(){
    if (isset($_SESSION['flag'])) {
        $_SESSION['isFacebookRegistration'] = 3;
    }
    $_SESSION['flag'] = true;
    $this->session = $_SESSION;
    return $this->render('template/index.twig');
}

This way, executing index() for first time won't change isFacebookRegistration value, but will set a flag. Next time, the conditial will be true an isFacebookRegistration will change.

viarnes
  • 2,018
  • 2
  • 19
  • 31