yii2tech/behavior-trait
最新稳定版本:1.0.0
Composer 安装命令:
composer require yii2tech/behavior-trait
包简介
Allows handling events via inline declared methods, which can be added by traits
关键字:
README 文档
README
Behavior Trait Extension for Yii 2
This extension provides the ability of handling events via inline declared methods, which can be added via traits.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist yii2tech/behavior-trait
or add
"yii2tech/behavior-trait": "*"
to the require section of your composer.json.
Usage
This extension introduces special trait [[\yii2tech\behaviortrait\BehaviorTrait]], which if used provides the ability of handling events via inline declared methods, which can be added via other traits. This trait can be added to any descendant of [[\yii\base\Component]].
Each event handler method should be named by pattern: '{eventName}Handler{UniqueSuffix}', where 'eventName' is a
name of the event the method should handle, 'UniqueSuffix' any suffix, which separate particular event handler
method from the others.
For example: if the class has an event 'beforeInsert' it can introduce method named beforeInsertHandlerEncryptPassword,
which will be automatically triggered when event 'beforeInsert' is triggered:
use yii\db\ActiveRecord; use yii2tech\behaviortrait\BehaviorTrait; class User extends ActiveRecord { use BehaviorTrait; // add `BehaviorTrait` allowing to use inline event handlers use EncryptPasswordTrait; // add trait, which introduce inline event handler // ... } trait EncryptPasswordTrait { public function beforeInsertHandlerEncryptPassword(Event $event) { if (!empty($this->newPassword)) { $this->password = sha1($this->newPassword); } } }
Attention: watch for the naming collisions, ensure any inline handler declared either in class or via trait has a unique name (with unique suffix)!
Note: using traits instead behaviors improves performance but is less flexible. Keep in mind that such approach has been rejected at Yii2 core at yiisoft/yii2#1041.
统计信息
- 总下载量: 302.92k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-07-02