setono / bot-detection-bundle
一个允许您测试用户代理是否为机器人的 Symfony 扩展包
v1.12.0
2024-02-26 08:35 UTC
Requires
- php: >=7.4
- setono/symfony-main-request-trait: ^1.0
- symfony/config: ^4.4 || ^5.4 || ^6.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.0
- symfony/http-foundation: ^4.4 || ^5.4 || ^6.0
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.0
- twig/twig: ^2.14 || ^3.4
Requires (Dev)
- infection/infection: ^0.26
- matomo/device-detector: ^5.0 || ^6.0
- matthiasnoback/symfony-dependency-injection-test: ^4.3
- nette/php-generator: ^3.6
- phpbench/phpbench: ^1.2
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- psalm/plugin-phpunit: ^0.18
- psalm/plugin-symfony: ^4.0
- roave/security-advisories: dev-latest
- setono/code-quality-pack: ^2.2
- symfony/console: ^4.4 || ^5.4 || ^6.0
- symfony/yaml: ^4.4 || ^5.4 || ^6.0
- webmozart/assert: ^1.11
This package is auto-updated.
Last update: 2024-09-23 08:05:41 UTC
README
检测用户代理是否为机器人并对其进行操作。在底层,这个扩展包使用 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 %}