Weave Code
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

Uap Php Laravel Package

ua-parser/uap-php

UA Parser for PHP: parse browser user agents into structured data (browser, engine, OS, device) using the uap-core regex database. Helpful for analytics, logging, and device detection; easy integration via Composer and built-in caching support.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require ua-parser/uap-php. Start by parsing a user agent string:

use UAParser\ParserFactory;

$parser = (new ParserFactory())->create();
$result = $parser->parse($userAgentString);

// Access parsed components
$userAgent = $result->userAgent;
$os = $result->os;
$device = $result->device;

Begin with common use cases: detecting mobile vs desktop (via $device->deviceName or $result->isMobile()), or browser family ($userAgent->family). Check UAParser\Result\Device, Result\OS, and Result\UserAgent classes for available properties.

Implementation Patterns

  • Middleware integration: Add user-agent parsing in a Laravel middleware to populate Request attributes (e.g., $request->uaDevice) for consistent access throughout the request lifecycle.
  • Logging & Analytics: Parse UAs during request logging to enrich analytics (e.g., track browser/device trends) using Laravel’s logging channels or custom logger.
  • Conditional rendering: In Blade, detect capabilities ($result->userAgent->major, $os->family) to serve tailored UI/UX (e.g., show "mobile app" CTA only for mobile users).
  • API responses: Include parsed device info in API responses for client-side analytics (ensure privacy compliance).
  • Caching: Use static caching via ParserFactory::createCached() (e.g., APCu or file-based) to avoid repeated parsing overhead in high-traffic apps.

Gotchas and Tips

  • Static caching is critical: Always use createCached() (not create()) in production—parsing is expensive without it. Defaults to FileCache (set UAPARSER_CACHE_DIR env var for control).
  • Data freshness: UA regexes become outdated quickly. Register for ua-parser/uap-core releases and update regularly via uap-core version pinning.
  • Missing fields: $result->userAgent->version may be null; always check null before formatting (e.g., sprintf('%s %s', $ua->family, $ua->version ?? 'Unknown')).
  • Device detection limitations: $device->deviceName is often generic (e.g., “Smartphone”); use $device->model and $device->brand for specificity—but note vendor reporting inconsistencies.
  • Debugging: Use dd($result) to inspect the full result object structure, as many properties (e.g., $os->versionParts) are arrays requiring careful handling.
  • Extension point: Override cache/backend by implementing UAParser\Cache\CacheInterface (e.g., Redis cache adapter) or extend Parser for custom logic.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4