This is the model.
public function genCode($width = 15)
{
$inputKey = $this->password();
$string = Yii::$app->security->generateRandomString($width);
$this->_encrypted = Yii::$app->security->encryptByKey( $string, $inputKey );
// $decrypted = Yii::$app->security->decryptByKey( $encrypted, $inputKey, $info = null );
return $this->_encrypted;
}
public function saveCodeSample()
{
$code = new Code;
$code->type = 'sample';
$code->owner_id = 1;
$code->value = $this->genCode();
return $code->save();
}
private function password()
{
$inputKey = 'averyrandomandverylongstring';
$this->_password = $inputKey;
return $this->_password;
}
Controller sample:
public function actionTest()
{
$codes = new CodesSetup;
return var_dump($codes->saveCodeSample());
}