setono / bot-detection-bundle
A Symfony bundle that allows you to test if a user agent is a bot
Package info
github.com/Setono/BotDetectionBundle
Type:symfony-bundle
pkg:composer/setono/bot-detection-bundle
Requires
- php: >=8.1
- symfony/config: ^6.4 || ^7.4 || ^8.0
- symfony/dependency-injection: ^6.4 || ^7.4 || ^8.0
- symfony/http-foundation: ^6.4 || ^7.4 || ^8.0
- symfony/http-kernel: ^6.4 || ^7.4 || ^8.0
- twig/twig: ^2.14 || ^3.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.42
- infection/infection: ^0.27.0 || ^0.32.0
- jangregor/phpstan-prophecy: ^2.0
- matomo/device-detector: ^5.0 || ^6.0
- matthiasnoback/symfony-dependency-injection-test: ^5.1 || ^6.3
- nette/php-generator: ^3.6 || ^4.1
- phpspec/prophecy-phpunit: ^2.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpstan/phpstan-webmozart-assert: ^2.0
- phpunit/phpunit: ^10.5
- rector/rector: ^2.0
- shipmonk/composer-dependency-analyser: ^1.8
- sylius-labs/coding-standard: ^4.1.1
- symfony/console: ^6.4 || ^7.4 || ^8.0
- symfony/yaml: ^6.4 || ^7.4 || ^8.0
- webmozart/assert: ^1.11
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-21 09:01:57 UTC
README
Detect if the user agent is a bot and act upon it. The detection is based on the bot list from matomo-org/device-detector, which is compiled into a single regular expression and updated weekly.
Before the full bot list is used, the user agent is checked against a short list of the most active crawlers (search engines, AI crawlers, SEO tools and social media fetchers). This makes it much faster to detect the most common bots and hence speeds up your request cycle.
Requirements
- PHP 8.1 or higher
- Symfony 6.4, 7.4 or 8
Installation
composer require setono/bot-detection-bundle
If you use Symfony Flex, the bundle is enabled automatically. Otherwise, add it to config/bundles.php:
<?php return [ // ... Setono\BotDetectionBundle\SetonoBotDetectionBundle::class => ['all' => true], ];
Usage
In your services
Inject BotDetectorInterface:
<?php use Setono\BotDetectionBundle\BotDetector\BotDetectorInterface; final class YourService { public function __construct(private readonly BotDetectorInterface $botDetector) { } public function yourAction(string $userAgent): void { // Checks the user agent of the current main request if ($this->botDetector->isBotRequest()) { // do something to this bot! } // Checks a user agent string if ($this->botDetector->isBot($userAgent)) { // ... } } }
isBotRequest() also accepts a Request object. It returns false if there is no request or the request has no
User-Agent header.
In Twig templates
{% if is_bot_request() %}
I knew you were a bot!
{% endif %}
{% if is_bot(user_agent) %}
This user agent is a bot
{% endif %}
Configuration
The list of the most active crawlers is the setono_bot_detection.popular_bots parameter. You can override it in
config/services.yaml, e.g. to put the bots that visit your site the most first:
parameters: setono_bot_detection.popular_bots: - Googlebot - bingbot - MyCustomCrawler
Each entry is a regular expression that is matched case-insensitively against the user agent. A user agent that matches an entry is treated as a bot, so keep the entries specific. A user agent that doesn't match any of them is still checked against the full bot list.