thefredfox / cakephp-ip-behavior
CakePHP 的 IpBehavior 插件
dev-master
2016-03-14 17:09 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-26 00:04:26 UTC
README
描述
CakePHP 数据库框架的 Ip Behavior,它将当前请求中获取的当前客户端 IP 填充到实体的指定字段。
此插件应与 IpType 插件很好地协同工作。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require thefredfox/cakephp-ip-behavior
之后,您需要在应用程序的引导文件中加载插件并将数据库类型映射如下
Plugin::load('IpBehavior', ['bootstrap' => true]);
在 Table 类本身中,您需要添加此行为
// in your Entity Table class (eg. UsersTable) public function initialize(array $config) { //... $this->addBehavior('IpBehavior.Ip'); //... }
默认情况下,该行为将使用名为 'ip' 的字段,但您可以按如下方式更改配置
// in your Entity Table class (eg. UsersTable) public function initialize(array $config) { //... $this->addBehavior('IpBehavior.Ip', ['fields' => ['other_ip_field']); //... }
如果您想确保 IP 总是被存储
// 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']); //... }
这样您就可以存储创建者和修改者的 IP
// in your Entity Table class (eg. UsersTable) public function initialize(array $config) { //... $this->addBehavior('IpBehavior.Ip', ['fields' => ['ip', 'other_ip_field' => 'always']); //... }
'new' 配置是默认的,您无需显式设置它。