thefredfox/cakephp-ip-behavior

IpBehavior plugin for CakePHP

dev-master 2016-03-14 17:09 UTC

This package is auto-updated.

Last update: 2024-04-25 23:08:51 UTC


README

Description

An Ip Behavior for the Database Framework of CakePHP, which fills a specified field of an entity with the current client ip taken from the current request.

This plugin should work lovely with the IpType plugin.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require thefredfox/cakephp-ip-behavior

After that you have to load the plugin in your application's bootstrap file and map the type for the database as follows:

Plugin::load('IpBehavior', ['bootstrap' => true]);

In the Table class itself you have to add this behavior:

// in your Entity Table class (eg. UsersTable)

public function initialize(array $config) {
    //...
    $this->addBehavior('IpBehavior.Ip');
    //...
}

The default is, that the behavior will use the field named 'ip', but you can change the configurations like this:

// in your Entity Table class (eg. UsersTable)

public function initialize(array $config) {
    //...
    $this->addBehavior('IpBehavior.Ip', ['fields' => ['other_ip_field']);
    //...
}

and if you want that the ip should always be stored:

// in your Entity Table class (eg. UsersTable)

public function initialize(array $config) {
    //...
    $this->addBehavior('IpBehavior.Ip', ['fields' => ['ip' => 'always']);
    // respectively
    $this->addBehavior('IpBehavior.Ip', ['fields' => ['other_ip_field' => 'always']);
    //...
}

In that way you can store the creators and the modifiers ip:

// in your Entity Table class (eg. UsersTable)

public function initialize(array $config) {
    //...
    $this->addBehavior('IpBehavior.Ip', ['fields' => ['ip', 'other_ip_field' => 'always']);
    //...
}

The 'new' configuration is default and you don't need to explicitly set it.