matomo/device-detector
Parses User-Agent strings and browser Client Hints to identify device type (desktop/tablet/mobile/TV/console), client apps (browsers, media players, feed readers), operating systems, and device brand/model. Composer-ready PHP library.
LaravelCache) and PSR-6/PSR-16 support. The Laravel-specific Cache\LaravelCache adapter ensures minimal friction in adoption.HTTP_USER_AGENT and optional Accept-CH headers. No database migrations or schema changes are needed.DeviceDetectorMiddleware) to parse UAs globally, reducing boilerplate in controllers.PSR6Bridge (e.g., Redis) to avoid redundant parsing.Accept-CH) add ~50–100ms latency on first request but improve accuracy. Requires server configuration (e.g., Nginx Accept-CH: Sec-CH-UA).spyc) or cache adapters (e.g., symfony/cache) may add dependencies.isMobile()) sufficient?HTTP_USER_AGENT alone adequate?PSR6Bridge with Laravel’s cache driver (e.g., Redis) for distributed caching.app/Http/Middleware/DeviceDetector.php).$this->app->singleton(DeviceDetector::class, function ($app) {
$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT'], ClientHints::factory($_SERVER));
$dd->setCache(new Cache\LaravelCache());
return $dd;
});
Http tests to mock $_SERVER['HTTP_USER_AGENT'] and verify detection logic.strpos() hacks) with the library’s methods.deprecated() helper.LaravelCache adapter manually.isMobile() → mobile-optimized flows).composer require matomo/device-detector.config/cache.php.DeviceDetectorService facade for clean syntax:
use DeviceDetector\DeviceDetector;
facade(DeviceDetectorService::class, DeviceDetector::class);
access.log).spyc → symfony/yaml).device_detection_unknown table for analysis:
if (!$dd->isBot() && empty($dd->getDeviceName())) {
\Log::warning("Unknown device UA: {$_SERVER['HTTP_USER_AGENT']}");
}
curl -I https://your-site.com).$dd->parse();
\Log::debug($dd->getAll());
autoload.php with spyc.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Cache backend failure (Redis down) | Increased parsing latency | Fallback to array cache (degraded mode). |
| Malformed UA string | Parsing errors | Sanitize input or wrap in try-catch. |
| Outdated YAML rules | False positives/negatives | Set up a cron job to auto-update rules. |
| Client Hints unsupported | Reduced accuracy | Fall back to HTTP_USER_AGENT only. |
| PHP process crashes | Lost in-memory cache | Use Redis for persistence. |
isMobile(), getClient()).DEVICE_DETECTION.md to the project with:
How can I help you explore Laravel packages today?