Install the Bundle Add the package via Composer:
composer require vipx/bot-detect-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Vipx\BotDetectBundle\VipxBotDetectBundle::class => ['all' => true],
];
Basic Usage
Inject the BotDetect service into a controller or service:
use Vipx\BotDetect\BotDetect;
public function __construct(private BotDetect $botDetect) {}
public function checkRequest(Request $request): bool
{
return $this->botDetect->isBot($request);
}
First Use Case: Block Bots from Forms Use middleware or a controller to block bot submissions:
public function submitForm(Request $request)
{
if ($this->botDetect->isBot($request)) {
return new Response('Bot detected. Access denied.', 403);
}
// Proceed with form handling...
}
Request-Based Detection
Attach the BotDetect service to middleware for global bot checks:
namespace App\Http\Middleware;
use Vipx\BotDetect\BotDetect;
class BotDetectionMiddleware
{
public function __construct(private BotDetect $botDetect) {}
public function handle(Request $request, Closure $next)
{
if ($this->botDetect->isBot($request)) {
return new Response('Bot detected.', 403);
}
return $next($request);
}
}
Register in app/Http/Kernel.php:
protected $middleware = [
// ...
\App\Http\Middleware\BotDetectionMiddleware::class,
];
Conditional Logic Use detection results to modify behavior (e.g., rate-limiting, CAPTCHA enforcement):
if ($this->botDetect->isBot($request)) {
$this->addCaptchaToForm();
}
Custom Bot Lists Extend the default bot list by overriding the service configuration:
# config/packages/vipx_bot_detect.yaml
vipx_bot_detect:
bot_list: ['%kernel.project_dir%/config/bots_custom.txt']
Event-Driven Integration Dispatch events when bots are detected (e.g., logging, analytics):
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
public function __construct(
private BotDetect $botDetect,
private EventDispatcherInterface $dispatcher
) {}
public function handleRequest(Request $request)
{
if ($this->botDetect->isBot($request)) {
$this->dispatcher->dispatch(new BotDetectedEvent($request));
}
}
Outdated Bot Lists The package was last updated in 2018, so the bot list may not include modern crawlers (e.g., AI-driven scrapers). Supplement with:
bot_list.False Positives Some legitimate services (e.g., Slack, Discord webhooks) may be flagged. Whitelist them:
$this->botDetect->addWhitelist(['Slackbot', 'Discordbot']);
Performance Overhead Avoid running detection on every request if unused. Scope checks to critical endpoints (e.g., forms, APIs).
Configuration Quirks
bot_list config expects absolute paths to text files (one bot per line).config/packages/vipx_bot_detect.yaml. Override carefully.Log Detected Bots Add debug output to verify detection:
if ($this->botDetect->isBot($request)) {
$this->logger->debug('Detected bot:', [
'user_agent' => $request->headers->get('User-Agent'),
'ip' => $request->getClientIp(),
]);
}
Test with Known Bots Use tools like User-Agent Switcher to simulate bot requests during development.
Extend Detection Logic
Override the BotDetect service to add custom rules:
// src/Service/CustomBotDetect.php
use Vipx\BotDetect\BotDetect as BaseBotDetect;
class CustomBotDetect extends BaseBotDetect
{
public function isBot(Request $request): bool
{
if ($this->isKnownBot($request)) {
return true;
}
// Add custom logic (e.g., check for suspicious headers)
return false;
}
}
Register the service in services.yaml:
services:
Vipx\BotDetect\BotDetect: '@App\Service\CustomBotDetect'
Handle Edge Cases
$userAgent = $request->headers->get('User-Agent', '');
if (empty($userAgent)) {
return true; // Assume bot if no UA
}
Custom Bot Patterns
Extend the BotDetect class to support regex-based detection:
public function addCustomPattern(string $pattern): void
{
$this->customPatterns[] = $pattern;
}
Integration with Security Layers
Pair with Symfony’s Firewall or AccessControl to block bots at the infrastructure level:
# config/packages/security.yaml
security:
access_control:
- { path: ^/admin, roles: ROLE_USER, bot_detected: false }
Rate-Limiting Bots
Use the BotDetectedEvent to trigger rate-limiting:
$this->dispatcher->dispatch(new BotDetectedEvent($request));
// Listen for the event to apply rate limits
Analytics Integration Track bot attempts in tools like Google Analytics or custom dashboards:
if ($this->botDetect->isBot($request)) {
$this->analytics->track('bot_detected', [
'path' => $request->getPathInfo(),
'user_agent' => $request->headers->get('User-Agent'),
]);
}
How can I help you explore Laravel packages today?