hisorange / browser-detect
为Laravel提供的浏览器和移动设备检测包。
5.0.3
2024-02-05 08:21 UTC
Requires
- php: ^8.1
- jaybizzle/crawler-detect: ~1.2
- league/pipeline: ^1.0
- matomo/device-detector: ^6.0
- mobiledetect/mobiledetectlib: ~4.0
- ua-parser/uap-php: ~3.9
Requires (Dev)
- orchestra/testbench: ~7.0 || ~8.0
- php-coveralls/php-coveralls: ~2.0
- phpunit/phpunit: ~9.0 || ~10.0
This package is auto-updated.
Last update: 2024-09-05 09:45:27 UTC
README
浏览器检测 v5.0 由 hisorange 提供
这是一个易于使用的包,用于识别访问者的浏览器详细信息及设备类型。没有魔法,结果是由多个经过测试和开发的包生成的。
支持Laravel 4.0至10.x之间的所有版本;也在5.6至8.2之间的所有PHP版本上进行了测试。
如何安装
composer require hisorange/browser-detect
是的,它已经准备好供您使用! ^.^
如何使用
在您的代码中调用Browser
外观
use Browser; // Determine the user's device type is simple as this: $isMobile = Browser::isMobile(); Browser::isTablet(); Browser::isDesktop(); if (Browser::isMobile()) { // Redirect to the mobile version of the site. } // Every wondered if it is a bot who loading Your page? if (Browser::isBot()) { echo 'No need to wonder anymore!'; } // Check for common vendors. if (Browser::isFirefox() || Browser::isOpera()) { $response .= '<script src="firefox-fix.js"></script>'; } // Sometimes You may want to serve different content based on the OS. if (Browser::isAndroid()) { $response .= '<a>Install our Android App!</a>'; } elseif (Browser::isMac() && Browser::isMobile()) { $response .= '<a>Install our iOS App!</a>'; }
即使在您的blade模板中
@mobile <p>This is the MOBILE template!</p> @include('your-mobile-template') @endmobile @tablet <p>This is the TABLET template!</p> <link rel="stylesheet" href="tablet.css" title="Reduce the page size, load what the user need"> @endtablet @desktop <p>This is the DESKTOP template!</p> @enddesktop {-- Every result key is supported --} @browser('isBot') <p>Bots are identified too :)</p> @endbrowser
简单易懂,不是吗?
版本支持
以下矩阵已经由伟大的GitHub Actions持续测试!
自2013年以来,该包在所有可能的PHP/Laravel版本矩阵上运行测试。
独立模式,无需Laravel!
基于社区请求;现在您可以在不使用Laravel的情况下使用库。只需简单地使用Parser类作为静态对象。
use hisorange\BrowserDetect\Parser as Browser; if (Browser::isLinux()) { // Works as well! }
可用的API调用
对Browser
外观的每个调用都将代理到结果对象,因此以下信息也适用于您的结果,您可以使用[数组]语法来访问它们。
配置、定制
如果您正在使用Laravel,那么在安装后运行以下命令
# Will copy a config file to ~/app/config/browser-detect.php
php artisan vendor:publish
对于独立模式应用自定义配置
use hisorange\BrowserDetect\Parser; $browser = new Parser(null, null, [ 'cache' => [ 'interval' => 86400 // This will override the default configuration. ] ]); $result = $browser->detect();
由于该包旨在易于使用,因此没有太多可配置的。但是,您可以自定义缓存和安全值。
高级使用信息
代码设计为易于使用风格,因此您对Browser
外观的每次调用都将访问结果对象并为您提供数据,但您也可以解析除当前用户代理以外的代理。
// When You call the detect function You will get a result object, from the current user's agent. $result = Browser::detect(); // If You wanna get browser details from a user agent other than the current user call the parse function. $result = Browser::parse('Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14');
值得注意!解析器只解析每个用户代理字符串一次,然后将其缓存,它使用内存运行时缓存来处理单页加载中的多次调用;并且它将使用应用程序的缓存来持久化结果大约一周,这应该为您提供一个足够的缓存机制,使检测的成本低于0.02毫秒
,这是通过80,000个假访问进行测试的。
变更日志
在CHANGELOG文件中查看详细更改。