Difference between session and persistent of phalcon framework

Phanix
·
·
IPFS
·

Simply put, persistent objects cannot be accessed across classes, while session objects can.

The best use case for persistent objects is to store search form data, so you don't have to worry too much about changing pages. Take, for example, the searchAction code snippet below generated with the phalcon devtool.

 if ($this->request->isPost()) {
            $query = Criteria::fromInput($this->di, 'Testmodel', $_POST);
            $this->persistent->parameters = $query->getParams();
        } else {
            $query = Criteria::fromInput($this->di, 'Testmodel', $_GET);
            $this->persistent->parameters = $query->getParams();
        }

In fact, persistent is a session bag object . If you do var_dump($this->persistent), you can see detailed information, and you can find that it is limited to a single class.

The default session object is to use the File adapter (Phalcon\Session\Adapter\Files). Although it is said that the official file access method requires the get() & set() method, it is actually directly used as a general parameter for access. It doesn't seem to be a problem (of course it is better to develop a good habit). It is also possible to check for existence with isset(), a built-in php function, or with the has() method of the session object.

 // Both ways of writing can be used, but it is recommended to use set to write $this->session->set("customerid", 1);
$this->session->customerid= 1;
 // Both ways of writing can be used, but it is recommended to use get to read echo $this->session->get("customerid");
echo $this->session->customerid;
 // Both of these can be written var_dump($this->session->has("customerid"));
var_dump(isset($this->session->customerid));

Original link: Phanix's Blog

CC BY-NC-ND 2.0

Like my work? Don't forget to support and clap, let me know that you are with me on the road of creation. Keep this enthusiasm together!