setono/bot-detection-bundle

一个允许您测试用户代理是否为机器人的 Symfony 扩展包

安装次数: 75,601

依赖者: 8

建议者: 0

安全: 0

星标: 6

关注者: 2

分支: 2

开放问题: 5

类型:symfony-bundle

v1.12.0 2024-02-26 08:35 UTC

README

Latest Version Software License Build Status Code Coverage Mutation testing

检测用户代理是否为机器人并对其进行操作。在底层,这个扩展包使用 matomo-org/device-detector,但它不是仅仅使用这个库,而是首先检查用户代理的一个非常小的子集(即主要搜索引擎)。这使得检测这些非常常见的机器人变得更加快速,从而加速了您的请求周期。

安装

composer require setono/bot-detection-bundle

如果您使用 Symfony Flex,这将自动安装并启用插件。如果不使用,请手动将扩展包添加到 bundles.php

使用方法

您可以在您的服务中使用机器人检测器

<?php

use Setono\BotDetectionBundle\BotDetector\BotDetectorInterface;

final class YourService
{
    private BotDetectorInterface $botDetector;

    public function __construct(BotDetectorInterface $botDetector)
    {
        $this->botDetector = $botDetector;
    }

    public function yourAction(): void
    {
        if ($this->botDetector->isBotRequest()) {
            // do something to this bot!
        }

        // ...
    }
}

并且您可以在您的 twig 模板中使用它

{% if is_bot_request() %}
    I knew you where a bot!
{% endif %}