rcordobar19 / activity-log
一个简洁的Laravel 5活动记录器,用于监控网站或Web应用程序上的用户活动。
此包的规范存储库似乎已消失,因此该包已被冻结。
Requires
- php: >=5.4.0
- laravel/framework: >=5.2.0
- regulus/tetra-text: 0.6.*
README
一个简单且整洁的Laravel 5活动记录器,用于监控网站或Web应用程序上的用户活动。
安装
基本安装、服务提供商注册和别名设置
要安装ActivityLog,请确保 "regulus/activity-log" 已添加到Laravel 5的 composer.json 文件中。
"require": {
"rcordobar19/activity-log": "dev-master"
},
然后从命令行运行 php composer.phar update。Composer将安装ActivityLog包。现在,您只需注册服务提供商并设置ActivityLog的别名。在 app/config/app.php 中,将以下内容添加到 providers 数组
Rcordoba\ActivityLog\ActivityLogServiceProvider::class,
并将以下内容添加到 aliases 数组
'Activity' => Rcordoba\ActivityLog\Models\Activity::class,
发布迁移和配置
要从命令行发布此包的配置和迁移,请运行以下命令
php artisan vendor:publish
如果您想自定义ActivityLog的配置,现在可以编辑 config/log.php 中的配置文件。
注意:迁移仅被发布;记得在准备好时运行它们。
要从命令行运行迁移以创建ActivityLog的表,请运行以下命令
php artisan migrate
基本使用
记录用户活动
Activity::log([ 'contentId' => $user->id, 'contentType' => 'User', 'action' => 'Create', 'description' => 'Created a User', 'details' => 'Username: '.$user->username, 'updated' => (bool) $id, ]);
上述代码将为当前登录的用户记录活动。IP地址将自动保存,如果用户将 developer 会话变量设置为 true,则将设置 developer 标志。这可以用来区分开发者和网站管理员的操作。如果将 updated 布尔值设置为 true,则将在 description 和 details 字段中将 "创建" 或 "添加" 的所有实例替换为 "更新"。
高级使用
从版本 0.6.0 开始,ActivityLog内置了根据Laravel的语言文件中的语言键动态创建描述的能力。如果您想在调用 log() 函数时不必将 language_key 设置为 true 以启用此功能,请在配置文件中将 defaults.language_key 设置为 true(默认情况下它不存在,因此您需要添加它)。
使用语言键记录用户活动
Activity::log([ 'contentType' => 'Record', 'description' => [ 'created_items', [ // "activity-log::descriptions.created_items" is ":user created :number :items." 'number' => 2, 'items' => 'SPL|labels.record', // "labels.record" in this example has a string of "Record|Records" ], ], 'details' => [ 'record', 'This is Some Kind of Record', ], 'data' => [ 'category' => 'Content', ], ]); echo $activity->getDescription(); // may output "Unknown User created 2 records." echo $activity->getDetails(); // may output "Record: This is Some Kind of Record" echo $activity->getData('category'); // will output "Content"
在上面的示例中,描述中的 items 替换变量在 | 字符之前有多个指定属性,该字符将它们与实际的语言变量分开。可用的属性如下
S - Return the "singular" string (in the case of "labels.record", "Record")
P - Return the "plural" string (in the case of "labels.record", "Records")
A - Prepend the string with "a" or "an" (example: "a record" instead of just "record")
L - Convert the string to lowercase
在上面的示例中,您将看到单数 ("S") 和复数 ("P") 都在使用。当两者都使用时,描述构建器将查找 number 替换变量以确定是否使用单数形式还是复数形式。
注意:
user替换变量将自动根据记录的用户ID设置。
获取链接内容
设置 content_types 配置数组,如下面的示例所示
'item' => [ 'uri' => 'view/:id', 'subdomain' => 'items', 'model' => 'App\Models\Item', ],
您可以使用getContentItem()根据指定的模型获取项目(假设在上面的示例中,您的内容类型设置为"Item")。您还可以使用getUrl()获取内容项目的URL或使用getLinkedDescription()获取项目的链接描述。
显示操作图标
根据配置设置显示操作图标
echo $activity->getIconMarkup();
您还可以使用getIcon()获取仅图标类,以便构建自己的图标标记。