whichbrowser/parser
PHP user-agent parser for browser sniffing (use sparingly). WhichBrowser/Parser identifies browser, engine, OS and device with names and versions. Works on PHP 7.0+ including PHP 8; useful for analytics or UX edge cases.
Strengths:
Weaknesses:
Key Use Cases in Laravel:
browser/device metadata for logging (e.g., Laravel Scout, Sentry).curl, Python-requests) for rate-limiting.Laravel-Specific Pros:
BrowserParserMiddleware to attach parsed data to the Request object (e.g., $request->browser->isMobile()).AppServiceProvider for global access (e.g., app('browser_parser')).@if(browser('safari')) ... @endif) for frontend logic.UserAgentParserInterface) for unit tests.Challenges:
cache()->remember() or Redis.map module) if possible.curl detection via Request::userAgent()).| Risk Area | Severity | Mitigation Strategy |
|---|---|---|
| Stale browser data | High | Schedule monthly dependency updates. |
| Performance bottlenecks | Medium | Implement request-level caching. |
| Middleware conflicts | Low | Test with other middleware (e.g., auth). |
| False positives | Medium | Combine with IP-based bot detection. |
| PHP version drift | Low | Pin to ^8.1 in composer.json. |
Accuracy vs. Performance Tradeoff:
Update Cadence:
Caching Strategy:
Alternatives Evaluation:
laravel-user-agents): May offer tighter Laravel integration.Compliance:
strpos(), regex, or no parsing).composer require whichbrowser/parser.// app/Http/Middleware/BrowserParser.php
public function handle(Request $request) {
$parser = new \WhichBrowser\Parser($request->userAgent());
$request->merge(['browser' => $parser]);
return next($middleware);
}
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(\WhichBrowser\Parser::class, function () {
return new \WhichBrowser\Parser(request()->userAgent());
});
}
@browser('mobile')).cache()->remember()).actingAs(), json()).php-json (for parsing) and php-mbstring (for Unicode handling).README.md section in the repo for:
WhichBrowser\Parser logs to Laravel’s monolog for tracking.How can I help you explore Laravel packages today?