mobiledetect/mobiledetectlib
Lightweight PHP library to detect mobile devices and tablets using User-Agent and HTTP headers. Provides simple checks like isMobile() and isTablet(), regularly updated with device rules, and easy to install via Composer for any PHP app.
App\Http\Middleware\DetectMobileDevice) to trigger mobile-specific logic (e.g., redirecting to a mobile-optimized route or loading device-aware assets).Illuminate\Cache).Detection\MobileDetect in the container, enabling dependency injection into controllers/services.MobileDetectMiddleware that attaches device metadata to the request object).MobileDetect::isTablet()) for cleaner syntax, mirroring Laravel’s Eloquent or Cache facades.Illuminate\Http\Request with a macro to auto-detect devices (e.g., Request::isMobile()).DeviceDetectorService) called later in the request lifecycle?Cache::evictExpired()) is critical for queue workers (e.g., Horizon).composer require mobiledetect/mobiledetectlib:^4.8
4.8.x) to avoid breaking changes.// app/Providers/MobileDetectServiceProvider.php
public function register()
{
$this->app->singleton(Detection\MobileDetect::class, function () {
$config = [
'autoInitOfHttpHeaders' => true,
'cacheKeyFn' => fn($key) => sha1($key), // Laravel-friendly
];
return new Detection\MobileDetect($config);
});
}
// app/Http/Middleware/DetectMobileDevice.php
public function handle(Request $request, Closure $next)
{
$detect = app(Detection\MobileDetect::class);
$request->merge([
'isMobile' => $detect->isMobile(),
'isTablet' => $detect->isTablet(),
'device' => $detect->getDeviceName(),
]);
return $next($request);
}
// app/Facades/MobileDetect.php
public static function isMobile(): bool
{
return app(Detection\MobileDetect::class)->isMobile();
}
$cache = new \Detection\Cache\RedisCache(
new \Redis(),
$defaultTTL = 3600 // 1 hour
);
$detect = new Detection\MobileDetect($cache);
$cache = new \Detection\Cache\ApcuCache($defaultTTL = 60);
Illuminate\Http\Request for fluent access (e.g., request()->isMobile()).MobileDetectMiddleware to gate routes (e.g., Route::middleware(['mobile'])->group(...)).@mobile directive for conditional UI rendering.setHttpHeaders() if behind a CDN (e.g., Cloudflare).setUserAgent() for testing or non-browser clients.isMobile()).DeviceFeatureService).Feature::mobile()).4.8.x) are safe; major updates (e.g., 4.9.x) require testing.How can I help you explore Laravel packages today?