tesis/laravel-parse-user-agent
Composer 安装命令:
composer require tesis/laravel-parse-user-agent
包简介
Get OS, device, browser from user_agent
README 文档
README
A Laravel package for parsing user-agent strings to identify device type, operating system, and browser details.
Features
- Identifies operating system (OS) from the user-agent string.
- Identifies the browser and its version.
- Determines the device type (Desktop, Mobile, Tablet).
Installation
You can install the package via Composer:
composer require tesis/laravel-user-agent-parser
Configuration
If you're using Laravel 5.5 or later, the service provider will automatically be registered. For earlier versions, you'll need to add the service provider manually.
Add the service provider to the providers array in config/app.php:
'providers' => [ // Other service providers... Tesis\LaravelUserAgentParser\Providers\UserAgentParserServiceProvider::class, ],
Usage
Parsing User-Agent Strings
You can use the LaravelUserAgentParser service to parse a user-agent string and get details about the device, OS, and browser.
Handling Requests in a Controller
You can integrate the parser directly into your Laravel controllers to handle HTTP requests:
use Illuminate\Http\Request; use Tesis\LaravelUserAgentParser\Services\UserAgentParser; class UserAgentController extends Controller { protected UserAgentParser $userAgentParser; public function __construct(UserAgentParser $userAgentParser) { $this->userAgentParser = $userAgentParser; } public function index() { $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'; $parsedData = $this->userAgentParser->parse($userAgent); return response()->json($parsedData); } }
Testing
To ensure everything is working correctly, you can run the package's tests using PHPUnit.
First install Orchestra
composer require --dev orchestra/testbench:"^7.0" --with-all-dependencies
then install phpunit on top of it (later version seems not to be working):
composer require --dev phpunit/phpunit
vendor/bin/phpunit
As we are building a Laravel package, the first thing we will need is Laravels Support package, so install that using the following composer command:
composer require illuminate/support
Note: only add it after installing orchestra and phpunit with particular version, otherwise it will be the last version and orchestra and phpunit might have conflicts.
and then in tests you can use it:
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { // }
Feature Tests
If your package includes HTTP endpoints, you can test them using feature tests.
public function testUserAgentParsingEndpoint() { $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'; $response = $this->postJson('/parse-user-agent', [ 'user_agent' => $userAgent, ]); $response->assertStatus(200) ->assertJson([ 'device_type' => 'Desktop', 'os' => 'Windows', 'browser' => 'Chrome', 'browser_version' => '58.0.3029.110', ]); }
Static analysis with PHPStan
composer require --dev phpstan/phpstan
Add phpstan.neon:
parameters: level: 9 paths: - src
and run:
./vendor/bin/phpstan analyse
Contributing
Contributions are welcome! Please see CONTRIBUTING for details.
Security Vulnerabilities
If you discover a security vulnerability within this package, please send an email to your-email@example.com. All security vulnerabilities will be promptly addressed.
License
This package is open-sourced software licensed under the MIT license.
tesis/laravel-parse-user-agent 适用场景与选型建议
tesis/laravel-parse-user-agent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tesis/laravel-parse-user-agent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tesis/laravel-parse-user-agent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-16