icm-services / activity-log
基于 Laravel 6 的 regulus/Activity-log 的分支。
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": {
"regulus/activity-log": "0.6.*"
},
然后从命令行运行 php composer.phar update
。Composer 将安装 ActivityLog 包。现在,您只需注册服务提供者并设置 ActivityLog 的别名。在 app/config/app.php
中,将以下内容添加到 providers
数组
Regulus\ActivityLog\ActivityLogServiceProvider::class,
并将以下内容添加到 aliases
数组
'Activity' => Regulus\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
字段中所有 "Create" 或 "Add" 的实例为 "Update"。
高级用法
从版本 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()
获取仅图标类的图标,然后您可以根据需要构建自己的图标标记。