hume/session-visits-bundle

此扩展用于跟踪 Symfony 会话的访问。

dev-main 2024-04-03 02:53 UTC

README

你好,这个扩展是我对如何计数和存储数据库中访问的思考。

SessionVisitsBundle 通过会话注册访问并刷新到数据库。该扩展跟踪访问、处理并存储数据库中的周、月、年访问。

包含组件

  • VisitsTracker
  • VisitsFinder
  • DateSystem

警告

目前,它只是我的一个测试沙盒,我在其中尝试新的技能和想法,因此不建议在生产环境中使用该扩展。

安装

SessionVisitsBundle 需要 php8.2Symfony7Doctrine-bundle2.12 来运行。请确保您的机器上已安装 Composer。

您可以通过 Composer 安装它

composer require hume/session-visits-bundle:dev-main

务必检查 config/bundle.php 中的此行是否与以下内容相同

<?php
// config/bundles.php
Hume\SessionVisitsBundle\HumeSessionVisitsBundle::class => ['dev' => true, 'test' => true],

注意

对于生产环境,请设置 ['all' => true],但正如我之前所说,不推荐这样做。

基本用法

您只需标记要跟踪访问的控制器的 Controller 即可。

<?php
// src/Controller/ExampleController.php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Hume\SessionVisitsBundle\Controller\VisitsTrackableController;

class ExampleController implements VisitsTrackableController
{
    public function indexAction(): Response
    {
        return new Response('Hello world!')
    }
}

就这样!您的控制器将由 VisitsTracker 订阅者 kernel.controller 监听。

提示

如果您想了解更多关于事件监听器和订阅者的信息,请查看:https://symfony.com.cn/doc/current/event_dispatcher.html

或构造函数注入

<?php
// src/Controller/ExampleController.php

declare(strict_types=1);

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Hume\SessionVisitsBundle\Component\VisitsTracker\VisitsTracker;

class ExampleController
{
    public function __construct(private visitsTracker $visitsTracker)
    {
        $visitsTracker->start();
    }
    
    public function indexAction(VisitsTracker $visitsTracker): Response
    {
        return new Response('Hello world!')
    }
}

从数据库获取访问

Session Visits Bundle 使用 Doctrine-Bundle 进行数据库操作。建议使用 Repository 来获取访问对象。

重要

请检查 Visits Repository 中的 find 和 sum 方法。检查访问模型在 Visits Entity

Repository

use Hume\VisitsSessionBundle\Repository\VisitsRepository;

DateSystem

注意

该部分正在建设中。

许可证

MIT