We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Session Variables Not Available to Volt after 4.0 Upgrade?

I'm upgrading a product from Phalcon 3.4.5 to 4.0. In 3.4.5, I create a session and set some session variable, such as the user's name and email address. I am then able to acess the session variables in my volt files like:

{% if session is defined and session.get('user').id is defined %}
    {% set support_full_name = session.get('user').first_name ~ ' ' ~ session.get('user').first_name  %}
    {% set support_email  = session.get('user').email %}
{% endif %}

After updating to Phalcon 4.0 I am unable to access the session variables in my volt files. "session" is not defined at all.

I'm creating the session in index.php using this code:

$di->set('session', function() use ($di) {
    $session = new Phalcon\Session\Manager();
    $files = new Phalcon\Session\Adapter\Stream();
    $session->setAdapter($files)->start();
    return $session;
});

And I'm setting the session data using:

    $session->set('user', (object) array(
        'id' => $user->id,
        'email' => $user->email,
        'first_name' => $user->first_name,
        'last_name' => $user->last_name,
    ));

I have verified that the session data is created and data is set. I can access the session data in my PHP files and I've dumped the data and it is set.

I've searched and can't find anything obvious, so I assume I'm suffering from "Excessive Bark up My Nose Syndrome". I'm hoping someone can help me see what I can't see.

Am I missing something that I have to do in 4.0 to make the session data accessible to Volt?



3.8k
Accepted
answer
edited Feb '20

OK, I understand the issue now.

In the 3.4.5 version of the code, I never used "if session is defined". Apparently I thought I was being smart by adding that check to see if I had ANY session data. My 3.4.5 version just had:

{% if session.has('user').id is defined %}

And that works in both 3.4.5 and 4.0.

So the issue is I can't check to see is "session" as a whole has been defined in ether release.

So, yeah, probably a "feature" and not a "bug" at this point! :D

But good to know for future coding.