Skip to content

Setono/BotDetectionBundle

Repository files navigation

Symfony Bot Detection Bundle

Latest Version Software License Build Status Code Coverage Mutation testing

Detect if the user agent is a bot and act upon it. Under the hood this bundle uses the matomo-org/device-detector, but instead of just using that library this library has a very small subset of user agents that it checks first (i.e. major search engines). This makes it much faster in detecting those very common bots and hence speeds up your request cycle.

Installation

composer require setono/bot-detection-bundle

This installs and enables the plugin automatically if you're using Symfony Flex. If not, add the bundle manually to bundles.php.

Usage

You can use the bot detector in your services:

<?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!
        }

        // ...
    }
}

and you can use it inside your twig templates:

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