vipx/bot-detect
Detect and identify web crawlers (Google, Bing, Yahoo, etc.) from user agent and IP. Loads bot metadata from YAML, returns matched bot details, and includes optional caching and configurable cache naming/dumping for better performance.
Illuminate\Http\Kernel::handle()) to block/flag bots preemptively.metadata_cache_file) aligns with Laravel’s caching systems (e.g., Illuminate\Cache), reducing metadata parsing overhead.config/bots.yml).FileCache or RedisCache can replace the default Symfony cache.$_SERVER['HTTP_USER_AGENT'] and $_SERVER['REMOTE_ADDR'] (standard in Laravel’s Request object).Metadata object (can be mapped to Laravel’s response modifiers, e.g., abort(403) for bots).namespace App\Http\Middleware;
use Vipx\BotDetect\BotDetector;
use Closure;
class DetectBots
{
protected $detector;
public function __construct(BotDetector $detector) { $this->detector = $detector; }
public function handle($request, Closure $next) {
$bot = $this->detector->detect($request->userAgent(), $request->ip());
if ($bot) { return response('Blocked', 403); }
return $next($request);
}
}
config() caching.config('bot-detect.whitelist') for custom rules.App\Exceptions\Handler to log blocked IPs.storage/framework/cache)?BotDetector as a singleton in AppServiceProvider:
$this->app->singleton(BotDetector::class, function ($app) {
$loader = new YamlFileLoader(new FileLocator());
return new BotDetector($loader, __DIR__.'/../Resources/metadata/extended.yml', [
'cache_dir' => storage_path('framework/cache/bot-detect'),
]);
});
config/bots.php for customization.BotDetected events for analytics (e.g., event(new BotDetected($bot));).symfony/yaml (no additional composer install needed).cache() helper to replace Symfony’s cache system.composer require vipx/bot-detect.config()-compatible JSON for easier maintenance.php artisan bot:cache:warm) to pre-load metadata.symfony/yaml:^5.4).storage/framework/cache is writable.bot-detect config file (publishable via service provider).DetectBots middleware before route handling (e.g., in $middlewareGroups['web']).BotDetected in EventServiceProvider for analytics.Log facade to track detections.config/bots.php.php artisan cache:clear or manually delete storage/framework/cache/bot-detect/*.BotDetector to log mismatches:
$detector = new BotDetector($loader, $metadataFile, ['debug' => true]);
dd() or Log::debug() to inspect $bot objects.$detector = new BotDetector($loader, $metadataFile, [
'cache_dir' => null, // Disable file cache
'metadata_cache_file' => 'redis://cache/bot-metadata',
]);
BotDetectionJob) to avoid blocking requests.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Metadata file corruption | False negatives/positives | Use Laravel’s filesystem to validate YAML on boot. |
| Cache directory permissions | Detection failures | Set storage_path('framework/cache') to 0755. |
| Upstream dependency conflicts | Integration breaks | Pin Symfony/YAML versions in composer.json. |
| Bot list outdated | Missed detections | Automate updates via GitHub webhooks. |
| Redis cache failure (multi-instance) | Inconsistent detections | Fallback to local file cache. |
docs/integration.md.How can I help you explore Laravel packages today?