niladam/laravel-visits

一个用于在Eloquent模型或页面中记录访问的简单包

2.0.0 2023-09-05 17:13 UTC

This package is auto-updated.

Last update: 2024-09-05 20:20:46 UTC


README

Latest Version on Packagist Total Downloads

这是一个小包,允许您轻松记录对模型和/或自定义URL的访问,无论是通过包含的中间件还是通过提供的辅助函数。

此包将访问存储在单独的表中,因此不会膨胀您现有的表。

此包使用PHP的inet_ntopinet_pton函数将IP地址以二进制格式存储,从而减少您的数据库大小。

此包尝试保持访问表简单,仅存储以下数据

  • 访问者的IP地址
  • 可选的用户ID,如果访问者已认证
  • 平台(desktopphonetabletrobotother)(使用jenssegers/agent
  • 引用(如果有的话)
  • 被访问的实体
  • 被访问的URL(如果不是实体)- 通过从字符串中移除当前应用URL。

安装

您可以通过composer安装此包

composer require niladam/laravel-visits

使用以下命令发布迁移

php artisan vendor:publish --provider="Niladam\LaravelVisits\LaravelVisitsServiceProvider" --tag="migrations"

使用以下命令发布配置文件

php artisan vendor:publish --provider="Niladam\LaravelVisits\LaravelVisitsServiceProvider" --tag="config"

运行迁移

php artisan migrate --step

准备您的模型,实现CanHaveVisits接口和HasVisits特性。

以下是一个示例

<?php

namespace App\Models;

use Niladam\LaravelVisits\Concerns\CanHaveVisits; // <--- add this to your model
use Niladam\LaravelVisits\Concerns\HasVisitsTrait; // <--- add this to your model

class Product extends Model implements CanHaveVisits // <--- add this to your model
{
    use HasVisitsTrait; // <-- add this to your model
}

将包含的中间件添加到您的路由中

use Niladam\LaravelVisits\Middleware\RecordVisitsMiddleware;

Route::get('/products/{product}', function (Product $product) {
    return view('products.show', [
        'product' => $product,
    ]);
})->middleware(RecordVisitsMiddleware::class);

就这样。您的访问将自动记录。

手动记录访问

此包提供以下辅助函数,帮助您记录访问。

默认情况下,记录将异步进行,因此不会减慢您的应用程序。

要手动记录访问,请使用recordVisit辅助函数

$product = Product::latest()->first();

recordVisit($product);

$url = 'https://my-cool-domain.app/some-url';

recordVisit($url);

您还可以使用lvVisit辅助函数记录访问,其看起来像这样

$product = Product::latest()->first();

lvVisit(
    visitable: $product, 
    count: 1, // optional, defaults to 1
    referer: 'some-referer', // optional, defaults to null
    platform: 'desktop', // optional, defaults to null, and will be determined from the request
    ipAddress: '127.0.0.1', // optional, defaults to null, and will be set as the request IP or '127.0.0.1'
    async: true // optional, defaults to true. If set to false, the visit will be recorded synchronously
); // <-- This will dispatch a job to create the visit

$url = 'https://my-cool-domain.app/some-url';

lvVisit(
    visitable: $url, 
    count: 4, 
    async: true
); // <-- This will dispatch a job to create 4 visits.

检索访问

$product = Product::latest()->first();
$product->visits; // <-- returns a collection of visits

检索特定平台/类型的访问

此包包含所有平台的范围,因此您可以限制结果到特定平台。

$product = Product::latest()->first();
$product->visits()->desktop()->get(); // <-- returns a collection of visits from desktop users
$product->visits()->phone()->get(); // <-- returns a collection of visits from phone users
$product->visits()->tablet()->get(); // <-- returns a collection of visits from tablet users
$product->visits()->robot()->get(); // <-- returns a collection of visits from robots
$product->visits()->other()->get(); // <-- returns a collection of visits from other platforms
$product->visits()->mobile()->get(); // <-- returns a collection of visits mobile (phone + tablet) users
$product->visits()->entities()->get(); // <-- returns a collection of visits to entities
$product->visits()->urls()->get(); // <-- returns a collection of visits to URLs

检索IP地址(因为它以二进制格式存储)

$product = Product::latest()->first();
$product->visits->first()->ip; // <-- returns the IP address as a string

自定义

此包包含一个配置文件,允许您自定义作业、中间件和默认用户模型。

配置文件已发布到config/laravel-visits.php,如下所示

<?php

return [
    /**
     * When saving the URLs to the database, it might make sense to remove
     * the current application URL from the logged URL.
     */
    'remove_current_app_domain' => env('LARAVEL_VISITS_REMOVE_CURRENT_APP_DOMAIN', true),

    /**
     * When saving the URLs to the database, it might make sense to remove some strings
     * from it. You can define them here. These will be removed from the URL
     * that will be saved.
     */
    'replace_in_urls' => [],

    /**
     * Overwrite these values to use your own models, jobs and middleware.
     */
    'overwrites' => [
        'user_model' => '\App\Models\User',
        'model' => \Niladam\LaravelVisits\Models\Visit::class,
        'job' => \Niladam\LaravelVisits\Jobs\RecordVisitJob::class,
        'job_url' => \Niladam\LaravelVisits\Jobs\RecordVisitUrlJob::class,
        'middleware' => \Niladam\LaravelVisits\Middleware\RecordVisitsMiddleware::class,
    ],
];

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全

如果您发现任何与安全相关的问题,请通过电子邮件niladam@gmail.com而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件