bestyii/yii2-encrypter Encryption data for Yii2

YII2 Encrypter

  1. 安装
  2. 如何使用

兼容 mcrypt_encryptopenssl_encrypt

Latest Stable Version Total Downloads License


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist bestyii/yii2-encrypter "*"

or add

"bestyii/yii2-encrypter": "*"

to the require section of your composer.json file.



return [
    'components' => [
        'encrypter' => [
            'class' => 'bestyii\encrypter\Encrypter',
            'key' => '32bit string',
            'iv' => '32bit string',



You can now use the encrypter manually in any part of the application to either encrypt a string

\Yii::$app->encrypter->encrypt('string to encrypt');

or decrypt and encrypted string

\Yii::$app->encrypter->decrypt('string to decrypt');

The extension also comes with a behavior that you can easily attach to any ActiveRecord Model.

Use the following syntax to attach the behavior.

public function behaviors()
    return [
        'encryption' => [
            'class' => '\bestyii\encrypter\EncrypterBehavior',
            'attributes' => [

The behavior will automatically encrypt all the data before saving it on the database and decrypt it after the retrieve.

Keep in mind that the behavior will use the current configuration of the extension for the encryption.

0 0
1 follower
Yii Version: 2.0
License: BSD-3-Clause
Category: Others
Developed by: Yang
Created on: Aug 20, 2022
Last updated: a year ago
Packagist Profile
Github Repository

Related Extensions