Php yii2中的beforesave功能放在何处,如何在控制器中访问
我想在我的应用程序注册功能,如何创建哈希密码使用yii2 这是我的user.phpPhp yii2中的beforesave功能放在何处,如何在控制器中访问,php,authentication,yii2,Php,Authentication,Yii2,我想在我的应用程序注册功能,如何创建哈希密码使用yii2 这是我的user.php <?php namespace app\models; use Yii; use yii\base\NotSupportedException; use yii\db\ActiveRecord; use yii\base\Security; use yii\web\IdentityInterface; /** * This is the model class for table "users".
<?php
namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\base\Security;
use yii\web\IdentityInterface;
/**
* This is the model class for table "users".
*
* @property string $userid
* @property string $username
* @property string $password
*/
class User extends ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'users';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'password', 'password_hash','auth_key', 'password_reset_token'], 'required'],
[['username', 'password'], 'string', 'max' => 45],
[['auth_key'], 'string', 'max' => 32],
[['password_reset_token', 'password_hash'], 'string', 'max' => 255],
[['username'], 'unique']
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'password_hash' => 'Password hash',
'auth_key' => 'Auth Key',
'password_reset_token' => 'Password Reset Token',
];
}
/** INCLUDE USER LOGIN VALIDATION FUNCTIONS**/
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
$expire = \Yii::$app->params['user.passwordResetTokenExpire'];
$parts = explode('_', $token);
$timestamp = (int) end($parts);
if ($timestamp + $expire < time()) {
// token expired
return null;
}
return static::findOne([
'password_reset_token' => $token
]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;//sha1($password);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($password);//Security::generatePasswordHash($password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Security::generateRandomKey();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Security::generateRandomKey() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
}
我想在用户模型中使用setPassword函数,在这里我必须放置beforesave函数,我需要更改actionCreate函数
谢谢您的帮助。您应该将其放置在您的模型中,因为此方法位于
yii\db\BaseActiveRecord
中。如官方文件所述,将其覆盖:
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
// Place your custom code here
return true;
} else {
return false;
}
}
官方文件:
frontend/models
、common/models
和backend/models
。您不应修改供应商
文件夹下的任何内容,包括BaseActiveRecord
文件,所有更改将在下一次composer更新时丢失
@Ilyaskarim方法具有$insert
布尔参数,插入时具有true
值,更新时具有false
值。根据您的情况使用它。但是插入和更新的事件是分开的,从技术上讲,您可以在模型之外订阅它们,例如在行为中。
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
// Place your custom code here
return true;
} else {
return false;
}
}