Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Mobiledetectlib

Mobiledetectlib Laravel Package

mobiledetect/mobiledetectlib

MobileDetect is a lightweight PHP class that detects mobile devices and tablets using the User-Agent string plus specific HTTP headers. It requires no dependencies beyond PHP, supports all major frameworks, and ships with a large, regularly updated device database.

View on GitHub
Deep Wiki
Context7

Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Frequently asked questions about Mobiledetectlib
How do I install mobiledetect/mobiledetectlib in a Laravel application?
Install it with Composer using composer require mobiledetect/mobiledetectlib. There is no Laravel service provider or config file to publish because the library is framework agnostic and has no dependencies beyond PHP. In practice you register it yourself: bind Detection\MobileDetect (or better, your own wrapper service) in a service provider so controllers and jobs resolve it from the container. The whole install is a single Composer package with no extensions, credentials, or build step.
Which PHP and Laravel versions does mobiledetect/mobiledetectlib support?
The current 4.x line requires PHP 8.2 or newer as of version 4.10.0, so it runs on any Laravel release whose PHP floor is 8.2 or above. The 3.x LTS line still targets PHP 7.4 for older applications, and 2.x is deprecated and should not be used for new work. Because the package is framework agnostic, Laravel version support really depends on your PHP version rather than on the package itself.
What are typical use cases for mobile detection in a Laravel app?
Common uses are choosing a mobile or desktop Blade view, swapping asset payloads, applying lightweight feature gating, and tagging analytics with device class. It also works for optional redirects or content-negotiation style middleware at the request edge. Treat the result as one advisory input rather than a source of truth, and avoid making security or entitlement decisions from a User-Agent classification.
Should I wrap MobileDetect in my own Laravel service instead of injecting it directly?
Yes. Wrap Detection\MobileDetect in an application-owned class such as DeviceClassifier exposing isMobile(), isTablet(), and platform(), then bind that in the container. This keeps the vendor type in exactly one file so switching to Client Hints, a hosted detection API, or your own regex set later is a one-file change. It also keeps Detection types out of controllers, Blade templates, and domain services, and makes the classifier trivial to fake in tests.
Do I need to call setUserAgent() manually in Laravel?
Usually not, because autoInitOfHttpHeaders defaults to true and the class reads the request headers itself in a classic PHP request lifecycle. However, if you disable that option, run behind a proxy that rewrites headers, or execute in a persistent runtime, you must call setUserAgent() and setUserAgentHeaders() explicitly, for example with Laravel's $request->userAgent() and $request->headers. Since version 4.8.01 this explicit assignment is required when autoInit is off, so pass the values through your wrapper rather than relying on globals.
How do I test code that depends on MobileDetect in Laravel?
Because the class performs no I/O, the simplest approach is to call setUserAgent() with fixture User-Agent strings for representative devices, bots, and tablets. Better still, depend on your own DeviceClassifier interface and bind a fake or mock in the test container so your feature tests do not exercise the vendor library at all. Keep a small fixture set of real UAs, including an unrecognized one, to assert the desktop fallback behavior explicitly.
Is MobileDetect accurate enough for production traffic today?
It is a good heuristic, not a guarantee. User-Agent strings are lossy and spoofable, and they are increasingly degraded by Chrome's UA reduction, iOS Request Desktop Site, privacy-hardened browsers, and in-app webviews. The failure mode is silent: an unrecognized User-Agent falls through to a desktop classification, so measure accuracy against your own traffic before gating anything expensive. Combine the signal with Client Hints such as Sec-CH-UA-Mobile, CSS media and viewport queries, and client-side feature detection.
Does MobileDetect support caching, and is it safe under Laravel Octane?
Yes, it offers optional PSR-16 caching with a configurable cache key function, defaulting to sha1, which is worth enabling on high-traffic detection paths. In long-running runtimes such as Octane, RoadRunner, or Swoole, be aware that earlier versions accumulated cache entries without bound; GHSA-mgj4-qjmw-v56v introduced a bounded 1000-entry FIFO cache, so run a patched release and define a per-request reset or state strategy. The User-Agent is attacker-controlled, so keep the maximum User-Agent length setting (500 characters by default) in place and never treat the result as a security control.
What are the alternatives to MobileDetect for device detection in Laravel?
Alternatives include User-Agent Client Hints delivered via Sec-CH-UA headers, browscap data files, Matomo's device-detector, and hosted APIs such as 51Degrees or DeviceAtlas. Each trades accuracy, cost, and infrastructure differently: Client Hints are cheap but not universally sent, while hosted APIs are accurate but add a network dependency. Because MobileDetect is dependency-free and fast, many teams keep it as the default and isolate it behind a wrapper so an upgrade path stays open.
How is mobiledetect/mobiledetectlib maintained, and what breaks when upgrading?
The project keeps one rolling branch per major line, with 2.x deprecated, 3.x maintained as LTS for PHP 7.4, and 4.x as the active line, so pin a major version and track tags rather than a branch. Known breaking changes when moving from 2.x or 3.x include the namespace move from \Mobile_Detect to Detection\MobileDetect, the 4.8.01 requirement to set the User-Agent and headers explicitly, and the PHP floor rising to 8.2 in 4.10.0. If you subclass Detection\Cache\Cache, audit narrowed parameter types before upgrading, since that LSP change fatals at class load. Verify real-world adoption through Packagist download stats rather than dependent counts shown in registries.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections