briteweb / activity-log
从 regulus/activity-log 分支创建,以支持 Laravel 6。
Requires
- php: >=7.3.0
- laravel/framework: >=6.0.0
- regulus/tetra-text: 0.6.*
README
A simple and clean Laravel 5 activity logger for monitoring user activity on a website or web application.
安装
基本安装,服务提供者注册和别名设置
要安装 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()
获取仅包含图标类的图标,然后您可以根据自己的图标标记来构建。