- How do I install matomo/device-detector in a Laravel app?
- Run composer require matomo/device-detector, then instantiate DeviceDetector with the request User-Agent string. In Laravel you can wrap it in a service provider or middleware so controllers receive a ready-to-use detector. No migrations or database changes are required.
- What PHP and Laravel versions does matomo/device-detector support?
- The package requires PHP 7.4 or newer, which aligns with Laravel 8 and later. It is compatible with Laravel 8+ and can be used on Laravel 7 by wiring the LaravelCache adapter manually. Testing on PHP 8.2+ is recommended for performance gains.
- Does matomo/device-detector support Laravel cache drivers like Redis?
- Yes. DeviceDetector supports PSR-6 and PSR-16 caches, and includes a LaravelCache adapter plus PSR6Bridge for distributed stores such as Redis or Memcached. For production, use a shared cache to avoid repeated User-Agent parsing in each PHP process.
- How do I register DeviceDetector as a singleton in Laravel's service container?
- Register DeviceDetector as a singleton in a Laravel service provider using $this->app->singleton(...). Inside the closure, create a new DeviceDetector with the request User-Agent and ClientHints::factory($_SERVER), then set a cache such as Cache\LaravelCache. This gives you dependency injection and reusable detection logic across the app.
- Can I use matomo/device-detector in Laravel middleware to detect devices globally?
- Yes. Create a middleware such as DeviceDetectorMiddleware that parses the incoming User-Agent and optionally attaches the detector or detection results to the request. This avoids repeating parsing boilerplate in every controller. Keep the middleware lightweight and rely on a shared cache for high-traffic routes.
- How do I detect mobile vs desktop vs tablet with matomo/device-detector?
- Use the DeviceDetector methods such as isMobile(), isTablet(), isDesktop(), isTV(), and getDeviceType() after parsing. You can also read getBrandName(), getModel(), getOs(), and getClient() for more granular data. The library identifies device type, client, operating system, brand, and model from the User-Agent and optional Client Hints.
- Does matomo/device-detector support Client Hints and what latency does it add?
- DeviceDetector can parse browser Client Hints when they are available, which improves accuracy for modern browsers. Client Hints add roughly 50 to 100ms latency on the first request and require server configuration such as an Accept-CH header in Nginx. If you do not configure them, User-Agent parsing still works but may be less precise.
- How do I test device detection logic in Laravel HTTP tests?
- In Laravel HTTP tests, mock the User-Agent by setting $_SERVER['HTTP_USER_AGENT'] before dispatching the request or by sending a custom header. Then assert the detection result from your middleware or service. Include edge cases such as empty strings, malformed User-Agents, and known mobile or bot strings.
- Is matomo/device-detector fast enough for high-traffic production Laravel apps?
- Default array caching is process-local, so high-traffic Laravel apps should use PSR6Bridge with Redis or Memcached to avoid redundant parsing. The core package is lightweight, but YAML parsers or cache adapters may add dependencies. Profile parsing time with Laravel Debugbar and cache aggressively for production QPS.
- How accurate is bot detection and how do I handle custom or legacy user agents?
- Bot detection relies on a predefined list, so emerging bots may occasionally slip through. Accuracy depends on the YAML regex rules maintained upstream, and custom or legacy User-Agents may require manual rule additions or parser overrides. Plan who updates the library's device and bot rules and review detection results against a known dataset.