setono/bot-detection-bundle

A Symfony bundle that allows you to test if a user agent is a bot

Installs: 82 344

Dependents: 8

Suggesters: 0

Security: 0

Stars: 7

Watchers: 2

Forks: 2

Open Issues: 5

Type:symfony-bundle

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

README

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 %}