regulus/activity-log

一个干净简洁的 Laravel 5 活动日志,用于监控网站或 Web 应用的用户活动。

v0.6.9 2020-04-05 08:54 UTC

This package is auto-updated.

Last update: 2024-09-27 05:45:32 UTC


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

现在您可以根据需要编辑 config/log.php 中的配置文件以自定义 ActivityLog 的配置。

注意:迁移已发布;记得准备好时运行它们。

要运行迁移以创建 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,则将在 descriptiondetails 字段中将所有 "创建" 或 "添加" 实例替换为 "更新"。

高级用法

从版本 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() 获取仅包含图标类的字符串,以便构建自己的图标标记。