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

how to reference a session variable within a file outside of Controller, Model or View ?

I set a session variable inside a controller :

$this->session->set("navig_param",$livc_code);

Now I want to test if that session variable is not empty inside a php file outside of the Controller domain, Model domain and View domain. In fact this file is in a particular folder named lib at the same level as controllers, models and views. So how to test the session variable in this php file ?

Unless you are 'using' a Phalcon related class you can use vanilla php to access the session variable. Check like this:

    if(isset($_SESSION['navig_param']) && !empty($_SESSION['navig_param'])) {
        // Do Awesome Things
    }

This link might help you: https://docs.phalcon.io/en/latest/reference/session.html

if ($this->session->has('navig_param')) {
   $livc_code = $this->session->get('navig_param');
}


1.5k
edited Nov '15

This should be helpful outside the controller

$session = \Phalcon\Di::getDefault()->get('session');
if($session->has('navig_param')){
    $value = $session->navig_param;
}

I can't stress how much @vhoxha08's answer makes sense. Phalcon (and likely most other frameworks) doesn't re-invent the wheel when it comes to sessions - it just piggybacks on the built-in session functionality. There's no reason not to use vanilla PHP for this.