Hello,
I've been messing around with Phalcon for a bit now and I thought it was time to have a custom DI in my application.
When a user is logged in to my application, I want the users model to be available all over my application so I have it handy when I need it.
In the end I want to achieve something like $this->user->id
, $this->user->isLoggedIn()
etc
I have a multi-module application and in my Module.php I have the $di set like so:
$di->set('user', function(){
return new WebUser();
});
Pretty straight-forward.
In my WebUser this is the main part that should be responsible for returing me User model:
class WebUser extends \Phalcon\Mvc\User\Plugin
{
public $user = null;
const AUTH_UNKNOWN = 1;
const AUTH_SUCCESS = 2;
const AUTH_WRONG_CREDENTIALS = 3;
const AUTH_ATTEMPTS_EXCEEDED = 4;
const AUTH_BANNED = 5;
const AUTH_NOT_FOUND = 6;
public function __construct(){
if($this->user == null)
$this->user = $this->register();
return $this->user;
}
public function register(){
if($this->session->has('vdi') && $this->session->has('vcf')){
if(!$user = $this->check($this->session->get('vdi'), $this->session->get('vcf'))){
$this->logger->notice("Session intergity check FAIL");
$this->session->destroy();
$this->persistent->user = null;
} else {
$this->logger->debug("Session intergity check OK");
$this->persistent->user = $user;
}
}
return $this->persistent->user;
}
...
The problem though is that the user model is accessible if I do $this->user->user->id
which is not what I'm really wanting. I'm assuming this is due to the public $user
.
How could I have the user modal accessible without the extra user
between and access it like $this->user->id
and also be able to use all my functions in WebUser?
Thank-you in advance