darvinstudio/darvin-bot-detector-bundle
Installation Add the bundle via Composer:
composer require darvinstudio/darvin-bot-detector-bundle
Enable the bundle in config/bundles.php:
DarvinStudio\DarvinBotDetectorBundle\DarvinBotDetectorBundle::class => ['all' => true],
Configuration
Publish the default config (if needed) and update config/packages/darvin_bot_detector.yaml:
darvin_bot_detector:
enabled: true
user_agents: ['bot1', 'bot2'] # Customize bot detection patterns
First Use Case Detect bots in a controller:
use DarvinStudio\DarvinBotDetectorBundle\Detector\BotDetector;
class MyController extends AbstractController
{
public function index(BotDetector $detector)
{
if ($detector->isBot()) {
return $this->json(['message' => 'Bot detected!'], 403);
}
// Normal logic for humans
}
}
Request-Based Detection
Inject BotDetector into controllers/services to check requests:
if ($detector->isBot()) {
// Block, log, or redirect
}
Event Listeners
Use Symfony events (e.g., kernel.request) to globally block bots:
// src/EventListener/BotBlocker.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->isMasterRequest() && $this->detector->isBot()) {
$event->setResponse(new Response('Forbidden', 403));
}
}
Custom User-Agent Rules Extend detection logic via config or a custom detector:
# config/packages/darvin_bot_detector.yaml
darvin_bot_detector:
user_agents:
- 'Googlebot'
- 'Bingbot'
- 'CustomBotPattern'
Symfony Security Component
Integrate with AccessControl or Voter for granular permissions:
public function decide(AdvanceVote $vote)
{
if ($vote->getToken()->getUser() === null && $this->detector->isBot()) {
return AccessDeniedException::create();
}
return AccessDenied::NO;
}
Logging Log bot attempts for analytics:
if ($detector->isBot()) {
$this->logger->warning('Bot detected', ['user_agent' => $request->headers->get('User-Agent')]);
}
Performance Cache detection results if checking repeatedly (e.g., in loops):
$cache = $this->cache->get('bot_detected_' . $request->getClientIp());
if ($cache === null) {
$cache = $detector->isBot();
$this->cache->set('bot_detected_' . $request->getClientIp(), $cache, 3600);
}
Outdated Codebase
Limited Customization
No Rate Limiting
symfony/security or nelmio/cors for extra safety.Configuration Overrides
user_agents patterns.Verify User-Agent Headers Check if headers are correctly passed:
$userAgent = $request->headers->get('User-Agent');
dump($userAgent); // Debug missing/empty values
Test Edge Cases
Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)).Log False Positives/Negatives Use a logger to track misclassifications:
$this->logger->debug('Bot detection result', [
'is_bot' => $detector->isBot(),
'user_agent' => $request->headers->get('User-Agent'),
'ip' => $request->getClientIp(),
]);
Custom Detector Service Override the default detector:
# config/services.yaml
DarvinStudio\DarvinBotDetectorBundle\Detector\BotDetector:
arguments:
$userAgents: ['%kernel.debug% ? "DevBot" : []'] # Dynamic logic
Event-Driven Extensions Dispatch custom events when bots are detected:
// src/Event/BotDetectedEvent.php
class BotDetectedEvent extends Event
{
public function __construct(private string $userAgent) {}
public function getUserAgent(): string { return $this->userAgent; }
}
Trigger in BotDetector or a listener.
Database-Backed Rules Store/load bot patterns from a database:
$botPatterns = $this->botPatternRepository->findAll();
$detector = new BotDetector($botPatterns);
Regex Compilation Pre-compile regex patterns if performance is critical:
$pattern = '/Googlebot/i'; // Pre-compile in a service constructor
Avoid Overhead Skip detection for non-sensitive routes or trusted IPs:
if (!$this->security->isGranted('ROLE_TRUSTED_IP') && $detector->isBot()) {
// Block
}
How can I help you explore Laravel packages today?