mobiledetect/mobiledetectlib
Lightweight PHP class to detect mobile phones and tablets using User-Agent plus HTTP headers. Identify device type and specific platforms/brands for responsive content, analytics, or redirects. Widely used and actively maintained via tagged major branches.
Cache facade (e.g., Cache::remember()) for performance optimization via cached detection results. Compatible with Redis, Memcached, or file-based caching.MobileDetectMiddleware) to attach device metadata to requests (e.g., $request->attributes->add(['isMobile' => true])).mobile.detected event) for dynamic responses (e.g., redirecting to mobile app or serving AMP HTML).MobileDetect as a singleton in AppServiceProvider for dependency injection.MobileDetect facade (e.g., MobileDetect::isTablet()) for consistency with Laravel conventions.Illuminate\Http\Request with mobile detection methods (e.g., $request->isMobile()).session()->put('device_type', 'tablet')).Http tests (e.g., withHeaders(['User-Agent' => 'MobileUA'])).setHttpHeaders() explicitly in edge cases (e.g., behind proxies).Cache::remember('mobile_detect', 3600, fn() => ...)) is critical for high-traffic APIs.4.8.x (deprecated) or upgrade.Cache::has() behavior) may affect custom cache implementations.
Cache facade and PSR-16 adapters (e.g., Illuminate\Cache\CacheManager).Sec-CH-UA headers.User-Agent headers?
autoInitOfHttpHeaders = false and manually set headers via setHttpHeaders().setUserAgent() is called explicitly (library throws exceptions in non-HTTP contexts).HandleIncomingRequest middleware.Detection\MobileDetect as a singleton with optional config (e.g., cacheKeyFn, autoInitOfHttpHeaders).@mobile/@tablet directives for template logic.Meta::resource()->append(['device' => 'mobile'])).actingAsMobileUser() helpers.track('device_detected', ['type' => 'tablet']).Cache-Control headers.composer require mobiledetect/mobiledetectlib:^4.10./debug/mobile).$this->app->singleton(Detection\MobileDetect::class, function ($app) {
return new Detection\MobileDetect([
'cacheKeyFn' => fn($key) => sha1($key),
'autoInitOfHttpHeaders' => true,
]);
});
// app/Facades/MobileDetect.php
public static function isTablet(): bool { ... }
MobileDetectMiddleware to kernel.php:
protected $middleware = [
\App\Http\Middleware\MobileDetectMiddleware::class,
];
Illuminate\Http\Request:
Request::macro('isMobile', fn() => MobileDetect::isMobile());
@mobile/@tablet logic.Cache::remember():
$isMobile = Cache::remember('mobile_detect_' . $request->ip(), 3600, fn() => MobileDetect::isMobile());
users table for analytics.4.8.x.setUserAgent() calls.4.8.x for PHP 8.0–8.1).Cache facade is compliant).mobile_detect_failure events).4.x is LTS). Major versions require testing for:
4.8.x).Cache::has() fixes in 4.9.0).4.10.0).MobileDetect::getUserAgent() to inspect raw input.MobileDetect::setDebugMode(true) for verboseHow can I help you explore Laravel packages today?