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;
}
}