Question

http://docs.phalconphp.com/en/0.6.0/reference/session.html

Storing/Retrieving data in Session

From a controller, a view or any other component thats extends Phalcon\DI\Injectable you can access the session service and store items and retrieve them in the following way:

<?php
    class UserController extends Phalcon\Mvc\Controller
    {
        public function indexAction()
        {
            //Set a session variable
            $this->session->set("user-name", "Michael");
        }

        public function welcomeAction()
        {

            //Check if the variable is defined
            if ($this->session->has("user-name")) {

                //Retrieve its value
                $name = $this->session->set("user-name"); //here, set or get?
            }
        }
    }
Was it helpful?

Solution

You are correct. That was supposed to be a get (for retrieving records). The corrected example is:

http://docs.phalconphp.com/en/latest/reference/session.html

<?php

class UserController extends Phalcon\Mvc\Controller
{

    public function indexAction()
    {
        //Set a session variable
        $this->session->set("user-name", "Michael");
    }

    public function welcomeAction()
    {

        //Check if the variable is defined
        if ($this->session->has("user-name")) 
        {

            //Retrieve its value
            $name = $this->session->get("user-name");
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top