小编Dan*_*uin的帖子

CakePHP 3中表单字段的加密/解密

我希望在添加/编辑时加密一些表单字段,并在通过蛋糕查找时解密.以下是在v2.7.2中适用于我的代码:

core.php

Configure::write('Security.key','secretkey');
Run Code Online (Sandbox Code Playgroud)

应用程序/模型/ patient.php.

public $encryptedFields = array('patient_surname', 'patient_first_name');

public function beforeSave($options = array()) {
    foreach($this->encryptedFields as $fieldName){
        if(!empty($this->data[$this->alias][$fieldName])){
            $this->data[$this->alias][$fieldName] = Security::encrypt(
                $this->data[$this->alias][$fieldName],
                Configure::read('Security.key')
            );
        }
    }
    return true;
}

public function afterFind($results, $primary = false) {

    foreach ($results as $key => $val) {
        foreach($this->encryptedFields as $fieldName) {
            if (@is_array($results[$key][$this->alias])) {
                $results[$key][$this->alias][$fieldName] = Security::decrypt(
                    $results[$key][$this->alias][$fieldName],
                    Configure::read('Security.key')
                );
            }
        }
    }
    return $results;
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我必须用生成的模型实体和带有虚拟字段的afterFind方法替换$ this-> data [],但我不能把它们全部放在一起.

php cakephp cakephp-3.0

3
推荐指数
1
解决办法
3214
查看次数

标签 统计

cakephp ×1

cakephp-3.0 ×1

php ×1