pataar/browser-detect
Composer 安装命令:
composer require pataar/browser-detect
包简介
Browser & Mobile detection package for Laravel.
README 文档
README
A Laravel package to identify the visitor's browser, operating system, and device type. Results are powered by two well-tested detection libraries — no magic involved.
Fork notice: This is a maintained fork of hisorange/browser-detect by Varga Zsolt, which appears to be abandoned. Full credit to the original author for the design and initial implementation.
Changes from the original
- PHP 8.3+ and Laravel 11–13 support (dropped older versions)
- Removed
ua-parser/ua-parserandmobiledetect/mobiledetect— detection is now powered by fewer, better-maintained libraries
Requirements
- PHP 8.3+
- Laravel 11, 12, or 13 (or standalone without Laravel)
Installation
composer require pataar/browser-detect
That's it — Laravel auto-discovers the service provider and facade.
Usage
Facade
use Browser; if (Browser::isMobile()) { // Redirect to the mobile version of the site. } if (Browser::isBot()) { echo 'Bot detected!'; } if (Browser::isFirefox() || Browser::isOpera()) { $response .= '<script src="firefox-fix.js"></script>'; } if (Browser::isAndroid()) { $response .= '<a>Install our Android App!</a>'; } elseif (Browser::isMac() && Browser::isMobile()) { $response .= '<a>Install our iOS App!</a>'; }
Blade directives
@mobile <p>This is the MOBILE template!</p> @include('your-mobile-template') @endmobile @tablet <p>This is the TABLET template!</p> @endtablet @desktop <p>This is the DESKTOP template!</p> @enddesktop @browser('isBot') <p>Bots are identified too!</p> @endbrowser
Standalone (without Laravel)
use hisorange\BrowserDetect\Parser as Browser; if (Browser::isLinux()) { // Works without Laravel! }
Parsing a specific user agent
$result = Browser::detect(); // Current visitor $result = Browser::parse('Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14');
Results are cached in memory for the current request and optionally persisted via Laravel's cache (7-day TTL by default).
API Reference
Device detection
| Method | Returns | Description |
|---|---|---|
Browser::isMobile() |
bool |
Is this a mobile device? |
Browser::isTablet() |
bool |
Is this a tablet device? |
Browser::isDesktop() |
bool |
Is this a desktop computer? |
Browser::isBot() |
bool |
Is this a crawler / bot? |
Browser::deviceType() |
string |
One of: Mobile, Tablet, Desktop, Bot |
Browser::deviceFamily() |
string |
Device vendor (Samsung, Apple, Huawei, ...) |
Browser::deviceModel() |
string |
Device model (iPad, iPhone, Nexus, ...) |
Browser detection
| Method | Returns | Description |
|---|---|---|
Browser::browserName() |
string |
Human-friendly name (e.g. Firefox 3.6) |
Browser::browserFamily() |
string |
Vendor (Chrome, Firefox, Opera, ...) |
Browser::browserVersion() |
string |
Version string with trailing .0 trimmed |
Browser::browserVersionMajor() |
int |
Semantic major version |
Browser::browserVersionMinor() |
int |
Semantic minor version |
Browser::browserVersionPatch() |
int |
Semantic patch version |
Browser::browserEngine() |
string |
Rendering engine (Blink, WebKit, Gecko, ...) |
Browser::isChrome() |
bool |
Chrome or Chromium? |
Browser::isFirefox() |
bool |
Firefox? |
Browser::isOpera() |
bool |
Opera? |
Browser::isSafari() |
bool |
Safari? |
Browser::isEdge() |
bool |
Microsoft Edge? |
Browser::isIE() |
bool |
Internet Explorer (or Trident)? |
Browser::isIEVersion(int, op) |
bool |
Compare against a specific IE version |
Browser::isInApp() |
bool |
In-app browser (WebView, Twitter, WeChat)? |
Operating system detection
| Method | Returns | Description |
|---|---|---|
Browser::platformName() |
string |
Human-friendly name (e.g. Windows 10) |
Browser::platformFamily() |
string |
Vendor (Linux, Windows, Mac, ...) |
Browser::platformVersion() |
string |
Version string with trailing .0 trimmed |
Browser::platformVersionMajor() |
int |
Semantic major version |
Browser::platformVersionMinor() |
int |
Semantic minor version |
Browser::platformVersionPatch() |
int |
Semantic patch version |
Browser::isWindows() |
bool |
Windows? |
Browser::isLinux() |
bool |
Linux? |
Browser::isMac() |
bool |
macOS or iOS? |
Browser::isAndroid() |
bool |
Android? |
Configuration
In Laravel, publish the config file:
php artisan vendor:publish --provider="hisorange\BrowserDetect\ServiceProvider"
In standalone mode, pass a custom config array:
use hisorange\BrowserDetect\Parser; $browser = new Parser(null, null, [ 'cache' => [ 'interval' => 86400, // Cache TTL in seconds ], ]);
Available options:
| Key | Default | Description |
|---|---|---|
cache.interval |
10080 |
Cache TTL in seconds |
cache.prefix |
bd4_ |
Cache key prefix |
cache.device-detector |
null |
Cache driver for device-detector's internal cache. See config/browser-detect.php for examples. |
security.max-header-length |
2048 |
Max user agent length (DoS protection) |
Quality
This package aims for 100% test coverage and PHPStan level max with zero baseline errors.
Credits
This package was originally created by Varga Zsolt (hisorange). This fork is maintained by Pieter Willekens (pataar).
Detection is powered by:
- jaybizzle/crawler-detect — Bot and crawler detection
- matomo/device-detector — Comprehensive device, browser, and OS parsing
License
pataar/browser-detect 适用场景与选型建议
pataar/browser-detect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.85k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2025 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「browser」 「user agent」 「detect」 「mobile」 「user-agent」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pataar/browser-detect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pataar/browser-detect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pataar/browser-detect 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Give your JS App some Backbone with Models, Views, Collections, and Events.
Instrument headless chrome/chromium instances from php5.6
Selenium4 (WebDriver) driver for Mink framework
Custom laravel monolog logger for datadog logs management, both api and agent ways
The Message Submission Agent Diagnostics tool (msadiag) facilitates testing the compatibility of third party message submission agents.
Clusteer is a Puppeteer wrapper written for PHP, with the super-power of parallelizing pages across multiple browser instances.
统计信息
- 总下载量: 4.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-02