- How do I install this package in a Laravel project?
- Run `composer require matomo/device-detector` in your project root. No additional Laravel-specific setup is required for basic usage, though middleware or service provider integration is recommended for production.
- Does this package support Laravel’s caching systems (Redis, file, etc.)?
- Yes, it integrates with Laravel’s cache bridges via PSR-6/PSR-16. Configure caching in `config/services.php` to store parsed results and improve performance. Redis is ideal for distributed environments.
- Can I use this for bot detection in Laravel?
- Absolutely. The library includes bot detection capabilities. Parse the User-Agent with `$dd->isBot()` and use middleware to filter or log bot traffic before it reaches your routes.
- Which Laravel versions are officially supported?
- The package requires PHP 7.4+ and works with Laravel 8.x, 9.x, and 10.x. Check the [GitHub repo](https://github.com/matomo-org/device-detector) for version-specific compatibility notes.
- How do I integrate this with Laravel middleware?
- Create a middleware class that instantiates `DeviceDetector` with `$request->userAgent()`, calls `$dd->parse()`, and attaches results to `$request->merge(['device' => $dd])`. Register it in `app/Http/Kernel.php` under the `web` middleware group.
- Will this work with Client Hints (Accept-CH header) in Laravel?
- Yes, but you must configure your server to send the `Accept-CH` header. The library falls back gracefully to `HTTP_USER_AGENT` if Client Hints are unavailable. Test with headers like `Sec-CH-UA` for modern browsers.
- How accurate is device detection for edge cases (e.g., custom User-Agents, bots)?
- The library uses regex-based parsing with high accuracy for common devices/browsers. For custom User-Agents, extend the YAML parser rules or test with real-world samples. Bot detection may require tuning for false positives.
- Can I store detected device data in a Laravel database?
- Yes. Create Eloquent models for `DeviceDetection` and log parsed data (e.g., `user_agent`, `device_type`, `is_bot`). Use query scopes like `DeviceDetector::where('is_mobile', true)` for filtering.
- What are the performance implications of parsing User-Agents on every request?
- Regex parsing adds minimal overhead (~1–5ms per request). Mitigate this by caching results in Redis or a file cache. For high-traffic apps, lazy-load detection or pre-warm caches during off-peak hours.
- Are there alternatives to this package for Laravel?
- Alternatives include `mobile-detect` (lighter but less feature-rich) or `ua-parser` (similar functionality but less Laravel-optimized). This package stands out for its Client Hints support, modularity, and Laravel cache integration.