sourcequartet//visitor-log

用于记录Laravel 5所有访问者的包

此包的官方仓库似乎已不存在,因此该包已被冻结。

0.1.2 2015-08-08 08:16 UTC

This package is not auto-updated.

Last update: 2023-05-27 10:41:26 UTC


README

Scrutinizer Code Quality Build Status Code Climate

一个K.I.S.S(保持简单,傻瓜式)包,用于记录你的Laravel 5应用的访客。

此包允许您将访客的用户代理记录到数据库中,具有多种功能,可以判断用户是否正在您的网站上,访客是否为认证用户等 👍

它还包括基于jenssegers/laravel-agentMobile-Detect库,允许您识别访客的用户代理。

它基于JN-Jones/visitor-log的优秀基础,完全重新设计以供Laravel 5使用Repository模式和中件间。

安装

在安装任何东西之前,Laravel的'文件'驱动程序在会话存储中已知没有持久性,您应该通过另一个驱动程序来存储会话。然而,这个包已经被设计来通过IP覆盖会话sid以使文件驱动程序持久化,这不是理想的解决方案,但它可以工作。

$ composer require sourcequartet/visitor-log
$ php composer update

Laravel 5+的配置

将提供者添加到config/app.php

'providers' => [

    SourceQuartet\VisitorLog\VisitorLogServiceProvider::class,

],

您还可以添加Visitor类的外观

'aliases' => [

	'Visitor' => SourceQuartet\VisitorLog\VisitorLogFacade::class,

],

发布迁移和包配置

$ php artisan vendor:publish --provider="SourceQuartet\VisitorLog\VisitorLogServiceProvider"

添加必要的中间件(您必须这样做)

将此行添加到您的App/Http/Kernel.php

protected $middleware = [
        \SourceQuartet\VisitorLog\Middleware\VisitorMiddleware::class,
    ];

您还可以将其注册为路由中间件,但是因为这个包可以忽略其配置中的路由,所以由您选择。我建议不要这样做。

配置

  • onlinetime:访客仍然被保存的时间(分钟)
  • usermodel:将其设置为您使用的认证提供者
    • Laravel:该包将使用任何扩展Laravel Guard的包,或者将使用基本的Auth Guard
    • Sentinel:Visitor-Log将尝试使用Sentinel获取用户 -- NOT READY --
  • ignore:这是Visitor-Log将忽略的页面数组。例如 "admin/online"

API和VisitorLog类

Visitor类是一个绑定到并包含在服务容器中的单例接口

  • SourceQuartet\VisitorLog\Visitor\Visitor是VisitorManager接口
  • SourceQuartet\VisitorLog\Visitor\VisitorManager是VisitorManager的直接实例,其中存储了在将数据发送到存储库之前的逻辑。它需要Illuminate\Config\RepositoryIlluminate\Http\RequestJenssegers\Agent\Agent作为依赖项来构建。
  • SourceQuartet\VisitorLog\Contracts\Visitor\VisitorContract是VisitorRepository接口合同
  • SourceQuartet\VisitorLog\Visitor\VisitorRepository 是处理所有数据库层的 SourceQuartet\VisitorLog\VisitorModel 仓库,它需要构建 SourceQuartet\VisitorLog\VisitorModelIlluminate\Database\DatabaseManager,并与它的契约绑定,即:SourceQuartet\VisitorLog\Contracts\Visitor\VisitorContract
  • SourceQuartet\VisitorLog\VisitorModel 是访客表模型,它管理 sid 属性的记录,并由 Illuminate\Database\Eloquent\Model 扩展

外观和访客类方法

/** Will check whether the user with $id is online and registered as a visitor
 *  return false if the user is not an authentificated User, else, it returns true
 */
Visitor::checkOnline($id);

/** Find the current visitor using his address IP and retriving his unique SID,
 *  The SID is also stored into the session through Illuminate\Http\Request accessor
 */
Visitor::findCurrent();

/** Clear all visitor where the created_at timestamp is inferior at created_at minus config(visitor-log::onlinetime)
 *  Should not be called out of the scope of a custom Middleware.
 */
Visitor::clear($time);

/** loggedIn() - Will return all visitor that are currently authentificated through Auth or Sentinel
 *  guests() - Will return all visitor that are currently not authentificated through Auth or Sentinel
 */
Visitor::loggedIn();
Visitor::guests();

/** Find a visitor by his User id, if the user is online and authentificated, it'll return VisitorModel Collection
 *  If the $id isn't passed as an attribute, it'll throw an SourceQuartet\Exception\InvalidArgumentException, 
 *  If the user corresponding to the $id is not a visitor (offline), it'll return a null
 */
Visitor::findUser($id);

/** Find a visitor by his IP, if the user is online and authentificated, it'll return VisitorModel Collection
 *  If the $ip isn't passed as an attribute/invalid IP, it'll throw an SourceQuartet\Exception\InvalidArgumentException,
 */
Visitor::findByIp($id);

/** isUser() - Check if current Visitor is an Authentificated User, return bool (true if authentificated, false if not)
 *  isGuest() - Opposite method to isUser()
 *  getUseragent() - Get current Visitor stored Useragent
 */
Visitor::isUser();
Visitor::isGuest();
Visitor::getUseragent();

// Fetch all visitor logged into the database.
Visitor::all();

// Should not be used out of the scope of a Custom Middleware of for testing purposes.
Visitor::create(array $attributes);
Visitor::updateOrCreate(array $attributes);

// Find Visitor directly with his SID
Visitor::create($id);

// Should not be used out of the VisitorManager constructor or Custom Middleware
Visitor::setAgentDetector();

使用 jenssegers/laravel-agent

  • getAgentDetector() - 此方法将返回 Jenssegers\Agent\Agent 的实例

is('Agent') 方法

Visitor::getAgentDetector()->is('Windows'); // Check if UserAgent is Windows
Visitor::getAgentDetector()->is('Firefox'); // Check if UserAgent is Firefox
Visitor::getAgentDetector()->is('iPhone'); // Check if UserAgent is iPhone
Visitor::getAgentDetector()->is('OS X'); // Check if UserAgent is OS X

魔法 is 方法

Visitor::getAgentDetector()->isAndroidOS();
Visitor::getAgentDetector()->isNexus();
Visitor::getAgentDetector()->isSafari();
Visitor::getAgentDetector()->isMobile();
Visitor::getAgentDetector()->isTablet();

设置 UserAgent

有时,你可能需要手动遍历或设置要分析的 UserAgent,所以这很简单

$visitors = Visitor::all();
foreach($visitors as $visitor)
{
    Visitor::getAgentDetector()->setAgentToDetector($visitor->useragent);
    Visitor::getAgentDetector()->isAndroidOS();
}

或者,如果你需要加载当前的 UserAgent

// Just leave it empty
Visitor::getAgentDetector()->setAgentToDetector();
Visitor::getAgentDetector()->isAndroidOS();
```php

Or in the case of a single loading
```php
$visitor = Visitor::findUser($id);
Visitor::getAgentDetector()->setAgentToDetector($visitor->useragent);
Visitor::getAgentDetector()->isAndroidOS();

使用 MobileDetect 和 jenssegers\laravel-agent 的 AgentDetector 的其他功能

请查看 jenssenger\laravel-agent 文档 以获取有关如何使用此 UserAgent 检测器的更完整文档,如果你想在 VisitorLog 中使用内置功能,只需将 Agent::method() 替换为 Visitor::getAgentDetector()->method() 就可以了,这不是更简单的方法,如果你想要直接使用 Agent Facade,则将其注册为 jenssenger\laravel-agent,它是此包的依赖项,因此,你只需要按照安装说明进行操作即可使用它 👍

访客模型

访客模型还提供了一些属性

  • sid:用于识别访客的随机字符串
  • ip:访客的 IP 地址
  • page:访客所在的页面
  • useragent:访客的用户代理
  • user:访客的用户 ID