Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Accessing session variable from volt view

I am trying to display/hide a div based on a session variable (based on whether the user is logged in or not). How can i access a session variable in volt to check if it has been set?

Thanks in advance.



3.6k
Accepted
answer
edited Jun '14

In controller:

public function someAction()
{
    $this->session->set('your_variable', 'value');
}

In view (volt syntax):

{% if session.get('your_variable') == null %} {# variable is not set #}
Print something
{% else %} {# variable is set #}
Print something else
{% endif %}

You need also set a session in your $di:

$di->set('session', function() {
    $session = new Phalcon\Session\Adapter\Files();
    if (session_status() == PHP_SESSION_NONE) {
        $session->start();
    }
    return $session;
});

In controller:

```php public function someAction() { $this->session->set('your_variable', 'value'); } ```

In view (volt syntax):

``` {% if session.get('your_variable') == null %} {# variable is not set #} Print something {% else %} {# variable is set #} Print something else {% endif %} ```

You need also set a session in your $di:

```php $di->set('session', function() { $session = new Phalcon\Session\Adapter\Files(); if (sessionstatus() == PHPSESSION_NONE) { $session->start(); } return $session; }); ```

Thank you! this worked for me :D

You're welcome :)



3.0k

Hi, Marcin Szepczynski. Your answer was give many help to me. Thank you very much! 感谢你!