matomo/device-detector
PHP library to parse User-Agent strings and Browser Client Hints to detect device type (desktop/mobile/tablet/TV, etc.), client apps (browsers, media players, bots), operating systems, and hardware brand/model. Composer-installable and well tested.
Begin by installing the package via Composer: composer require matomo/device-detector. The core class is DeviceDetector, which accepts a user agent string (typically $_SERVER['HTTP_USER_AGENT']) and optional client hints (via ClientHints::factory($_SERVER)). After instantiation, call parse(), then use methods like isBot(), getDeviceName(), getOs(), getClient(), getBrandName(), and getModel() to extract details. For basic use, 5–10 lines of code suffice—no heavy configuration needed. Start by handling bots separately (isBot()) and fallback UI/UX based on device type (e.g., isTablet() vs isSmartphone()).
DeviceDetector early to skip processing for known bots ($dd->isBot()), reducing load.Accept-CH headers (e.g., User-Agent, Device-Memory, Sec-CH-UA-Platform) to improve accuracy—especially for Android 13+ devices and modern Chrome/Firefox browsers.new LaravelCache() for Laravel or new PSR16Bridge(new SimpleCache(...)) for generic setups—crucial because YAML pattern matching is expensive on every request.BotParser directly (or call $dd->skipBotDetection() and $dd->discardBotInformation() for speed).OperatingSystem::getOsFamily() and Browser::getBrowserFamily() to normalize version-agnostic groups (e.g., group all Firefox variants under “Firefox”).112 → 112.0). Use AbstractDeviceParser::setVersionTruncation(AbstractDeviceParser::VERSION_TRUNCATION_NONE) if you need full version numbers—but benchmark impact on memory/cache size.symfony/yaml or spyc is available; otherwise, parsing silently fails or throws. Prefer symfony/yaml for reliability and ongoing support.DeviceDetector instances with different configs (e.g., bot-skip vs full-mode), manually invalidate or use distinct cache keys.Accept-CH headers aren’t set server-side, client hints may be missing entirely—even on modern browsers. Validate ClientHints::factory($_SERVER) returns non-null before relying on it.discardBotInformation() for performance-critical paths, or whitelist known bots after initial analysis.null or malformed UA causes inconsistent results; validate first.ServiceProvider to bind a shared DeviceDetector instance, and cache it via Cache::remember() in your controller to avoid repeated instantiation and YAML parsing across requests.How can I help you explore Laravel packages today?