Phalcon 在所有控制器中保持模型持久性?
Phalcon keep a model persistant in all the controllers?
我的网站应用程序主要是围绕用户模型建模的,该模型具有大部分时间所需的所有关键数据。
一旦用户登录网站,我想将其作为所有控制器的持久变量。我如何实现这一点,因为我不能使用会话来保存类型模型的 class 对象。
我的应用程序是基于 phalcon 的。但是欢迎提出任何建议。
我建议你写一个简单的 class 用于用户身份验证和其他用户数据操作,我写了这个 Component
并在我的项目中使用:
use Phalcon\Mvc\User\Component;
class Auth extends Component {
public function login($credentials) {
if(!isset($credentials['email'],$credentials['password'])) {
return FALSE;
}
if($this->isAuthorized()) {
return true;
}
$user = Users::findFirstByEmail($credentials['email']);
if($user == false) {
//block user for seconds
return false;
}
if($this->security->checkHash($credentials['password'],$user->password) && $user->status == 1) {
$this->_saveSuccessLogin($user);
$this->_setUserLoginSession($user);
return true;
} else {
return false;
}
}
public function isAuthorized() {
return $this->session->has('auth');
}
public function logout() {
$this->session->remove('auth');
return true;
}
public function user($key = null) {
if(!$this->isAuthorized()) {
return null;
}
if(is_null($key)) {
return $this->session->get('auth');
} else {
$user = $this->session->get('auth');
return array_key_exists($key, $user) ? $user[$key] : null;
}
}
private function _saveSuccessLogin(Users $user){
$userLogin = new UserLogins();
$userLogin->user_id = $user->id;
$userLogin->ip = $this->request->getClientAddress();
$userLogin->user_agent = $this->request->getUserAgent();
$userLogin->dns = gethostbyaddr($userLogin->ip);
if(!$userLogin->save()) {
return false;
}
return true;
}
private function _setUserLoginSession(Users $user) {
if(!$user) {
return false;
}
$this->session->set('auth',array(
'id' => $user->id,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'email' => $user->email,
'role_id' => $user->role_id
));
return true;
}
}
并在我的 services.php
中使用以下代码添加到 DI
中:
$di->setShared('auth', function () {
return new Auth();
});
所以当我想获取用户信息时,我使用这个:
$this->auth->user('email')
您还可以向该组件添加更多功能并对其进行修改。
希望对您有用。
您可以使用 memcached 并将其保存为键 => 值:
userId => 序列化用户模型
我的网站应用程序主要是围绕用户模型建模的,该模型具有大部分时间所需的所有关键数据。
一旦用户登录网站,我想将其作为所有控制器的持久变量。我如何实现这一点,因为我不能使用会话来保存类型模型的 class 对象。
我的应用程序是基于 phalcon 的。但是欢迎提出任何建议。
我建议你写一个简单的 class 用于用户身份验证和其他用户数据操作,我写了这个 Component
并在我的项目中使用:
use Phalcon\Mvc\User\Component;
class Auth extends Component {
public function login($credentials) {
if(!isset($credentials['email'],$credentials['password'])) {
return FALSE;
}
if($this->isAuthorized()) {
return true;
}
$user = Users::findFirstByEmail($credentials['email']);
if($user == false) {
//block user for seconds
return false;
}
if($this->security->checkHash($credentials['password'],$user->password) && $user->status == 1) {
$this->_saveSuccessLogin($user);
$this->_setUserLoginSession($user);
return true;
} else {
return false;
}
}
public function isAuthorized() {
return $this->session->has('auth');
}
public function logout() {
$this->session->remove('auth');
return true;
}
public function user($key = null) {
if(!$this->isAuthorized()) {
return null;
}
if(is_null($key)) {
return $this->session->get('auth');
} else {
$user = $this->session->get('auth');
return array_key_exists($key, $user) ? $user[$key] : null;
}
}
private function _saveSuccessLogin(Users $user){
$userLogin = new UserLogins();
$userLogin->user_id = $user->id;
$userLogin->ip = $this->request->getClientAddress();
$userLogin->user_agent = $this->request->getUserAgent();
$userLogin->dns = gethostbyaddr($userLogin->ip);
if(!$userLogin->save()) {
return false;
}
return true;
}
private function _setUserLoginSession(Users $user) {
if(!$user) {
return false;
}
$this->session->set('auth',array(
'id' => $user->id,
'firstname' => $user->firstname,
'lastname' => $user->lastname,
'email' => $user->email,
'role_id' => $user->role_id
));
return true;
}
}
并在我的 services.php
中使用以下代码添加到 DI
中:
$di->setShared('auth', function () {
return new Auth();
});
所以当我想获取用户信息时,我使用这个:
$this->auth->user('email')
您还可以向该组件添加更多功能并对其进行修改。
希望对您有用。
您可以使用 memcached 并将其保存为键 => 值: userId => 序列化用户模型