ruskid / yii2-ip-behavior
Yii2 Behavior that records User IP address on Updates/Inserts
Installs: 8 898
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 3
Forks: 3
Open Issues: 2
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-11-07 01:13:39 UTC
README
Yii2 Behavior that records User IP address on Updates/Inserts
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require ruskid/yii2-ip-behavior "dev-master"
or add
"ruskid/yii2-ip-behavior": "dev-master"
to the require section of your composer.json
file.
Or
I wanted to test the composer xd. You can just copy paste IpBehavior.php to behaviors folder or any folder you like. (just don't forget to change the namespace).
Usage
You can call it like this.
public function behaviors() { return [ ... 'ip' => [ 'class' => IpBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_ip', 'updated_ip'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_ip', ], ] ... ]; }
Or like this. where value can be a string or an anonymous function that will return a string.
public function behaviors() { return [ ... 'ip' => [ 'class' => IpBehavior::className(), 'createdIpAttribute' => 'created_ip', 'updatedIpAttribute' => 'updated_ip', 'value' => '127.0.0.1', ] ... ]; }
Extras
this will set user's IP address to the attribute of the model.
$user->setIp('updated_ip');