Where from Phalcon/Session/Bag know about handling session?

Hello,

I'm sorry, but it is unclear for me - where from Phalcon/Session/Bag know how to store variables in session? How can we store Bag in session and how can we retrieve.

I've took a look at documentation http://docs.phalconphp.com/en/0.9.0/reference/session.html#session-bags, but was unable to find how to do it.

Thank you.



4.8k

It is written "By only setting the variables in the “bag”, it’s automatically stored in session", but how Bag know what session adapter we're using? Or, maybe Bag takes Session adapter from DI? But Bag know nothing about DI because we never used setDI() method.



83.4k

Maybe there is a typo in the docs, because you need to manually assign the DI to the Bag, or build a bag from DI to automatically assign it. Usually the session bags are used by accessing the "persistent" property in classes that extends Phalcon\DI\Injectable:

<?php

class UserController extends Phalcon\Mvc\Controller
{

    public function indexAction()
    {
        // Create a persistent variable "name"
        $this->persistent->name = "Laura";
    }

    public function welcomeAction()
    {
        if (isset($this->persistent->name))
        {
            echo "Welcome, ", $this->persistent->name;
        }
    }

}

Or

$user       = new Phalcon\Session\Bag();
$user->setDI($di);
$user->name = "Kimbra Johnson";
$user->age  = 22;

Or

$di['user']  = function(){
    return  new Phalcon\Session\Bag();
}