piwik/device-detector
Parse User-Agent strings and Browser Client Hints to identify device type (desktop/tablet/mobile/TV/console, etc.), browser/client, operating system, and device brand/model. Universal PHP library from Matomo for accurate device detection.
LaravelCache) and PSR-6/PSR-16 support, aligning with Laravel’s ecosystem (e.g., Symfony Cache, Redis, or database caching). The LGPL-3.0 license is compatible with Laravel’s MIT license.Spyc with Symfony’s YAML parser) without refactoring core logic.matomo/device-detector) with zero Laravel-specific setup (unlike packages requiring service providers).DeviceDetectorMiddleware) to parse $_SERVER['HTTP_USER_AGENT'] globally, reducing boilerplate.DeviceDetector bound to DeviceDetectorService), enabling reusable logic across controllers/services.isBot() checks to exclude crawlers from analytics.Cache::tags(['device-detector'])) for invalidation.Spyc parser is deprecated; migrate to Symfony’s YAML parser (included in Laravel) to avoid future breakage.LaravelCache bridge) or PSR-6 adapters (e.g., Redis, DynamoDB).Spyc with Symfony’s Yaml component (already in Laravel) for consistency.DeviceDetector into Laravel’s middleware to parse UAs globally (e.g., DetectDeviceMiddleware).HttpTests to mock $_SERVER['HTTP_USER_AGENT'] and verify detection logic.Phase 1: Proof of Concept (1–2 days)
composer require matomo/device-detector.use DeviceDetector\DeviceDetector;
$dd = new DeviceDetector($_SERVER['HTTP_USER_AGENT']);
$dd->parse();
dd($dd->isMobile(), $dd->getClient());
Phase 2: Middleware Integration (1 day)
app/Http/Middleware/DetectDevice.php:
public function handle(Request $request, Closure $next) {
$request->device = (new DeviceDetector($request->userAgent()))->parse();
return $next($request);
}
app/Http/Kernel.php.Phase 3: Caching & Optimization (1–2 days)
$dd->setCache(new DeviceDetector\Cache\PSR6Bridge(Cache::store('redis')));
Laravel Debugbar.Phase 4: Rule Customization (Ongoing)
DeviceDetector\Parser\Device\AbstractDeviceParser.yaml or symfony/yaml (for YAML parsing). No other extensions needed.| Step | Task | Dependencies | Owner |
|---|---|---|---|
| 1 | Add Composer dependency | None | Backend |
| 2 | Implement middleware | Step 1 | Backend |
| 3 | Test with real UAs | Step 2 | QA |
| 4 | Configure caching | Step 3 | DevOps |
| 5 | Integrate with analytics/personalization | Step 4 | PM/Dev |
composer.json (e.g., ^5.0) to avoid surprises.custom-rules.yml file for organization-specific devices. Update via CI/CD (e.g., GitHub Actions) when new devices are detected.dd($dd->getAll()) to inspect raw detection data. Log unknown UAs to a monitoring tool (e.g., Sentry).BotParser.DEVICE_DETECTION.md to the repo with:
DeviceDetector\Cache\FileCache) is not thread-safe; use Redis/Memcached in clustered environments.laravel-debugbar). Optimize by:
skipBotDetection() if bots are irrelevant.VERSION_TRUNCATION_NONE only if full versions are needed.Spyc; switch if using default.| Scenario | Impact | Mitigation |
|---|---|---|
| Cache Failure (e.g., Redis down) | Degraded performance (fallback to array cache) | Use multi-level caching (e.g., Redis → file cache). |
| YAML Parser Error | Detection fails entirely | Fallback to a minimal parser or log errors to Sentry. |
| New UA Not Detected | False negatives in analytics | Implement a feedback loop (e.g., log unknown UAs to a DB). |
| Client Hints Unavailable | Reduced accuracy for modern devices | Gracefully degrade to UA-only parsing. |
rules.yml (e.g., regex patterns for UAs).How can I help you explore Laravel packages today?