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

Phalcon keep a model persistant in all the controllers?

my website application is mostly model around a User Model which has all the key data that needed for most of the times.

Once the user is logged into the website I would like to keep it as a persistent variable across all the controllers. How do i achieve this as i cannot use session to hold a class object of Type Model.

I have tried this as a user component but failed to retrieve the model back on controllers and views, it gives null even if the user has been already logged in.

see below class, when it try to access it via $this->auth-->user in controllers it always gives null.

i load this via index.php on to DI

//sets the user service for all modules
$di->setShared('auth', function () {
    return new \Lxxx\Components\Auth();
});

see class

use Phalcon\Mvc\User\Component;

class Auth extends Component { public $user; public function login($login,$password) { if ($this->isAuthorized()) { return true; } $user = User::findFirstByName($login); if ($user) { $user->password = $this->security->hash($user->password); if ($this->security->checkHash($password, $user->password)) { $this->session->set("auth", true); $this->session->set("name", $user->name); $this->user=$user; return true; } else { return false; } } } public function user(){ return $this->user; } public function isAuthorized() { return $this->session->has('auth'); }

public function logout()
{
        // Remove a session variable
        $this->session->remove("auth");
        // Destroy the whole session
        $this->session->destroy();
        // A HTTP Redirect
        $this->session->remove('auth');
        return true;
}

}

This is perfect job for sessions. This is part from my login script:

$this->session->cms = (object) array(
    'id' => $user->id,
    'names' => $user->names,
    'username' => $user->username,
    'userType' => $user->user_type,
    'lastActivity' => $user->last_activity
);  


13.4k

I don't want to use sessions for this because I need to use model functions as well i.e. getAge(). and this model is pretty much large ...etc plus the other advantage of keeping the data as it is model object. ...

edited Oct '15

I dont you think can achieve that without session or other type of storage.

But you can store the model object in session like so (its up to you to decide if thats a good idea):


// find test user
$user = AdminUsers::findFirst(1);
$this->session->user = $user;

// random controller
public function indexAction()
{     
    echo $this->session->user->getAge();
    // 27 years old
}   

// model
class AdminUsers extends \Phalcon\Mvc\Model
{
    public function getAge() {
        return '27 year old';
    }
}

Instead of full object you can store just Array(values) of properties, save it in session or other storage(cache or anything service you want) and then you can create User object and assign this values to properties.