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

Php User Agent Bundle Laravel Package

exs/php-user-agent-bundle

Symfony bundle that wraps donatj/PhpUserAgent. Register EXSPhpUserAgentBundle, then call PhpUserAgentService::parseUserAgent() on a user-agent string to detect platform, browser, and browser version. Simple service-based access with no extra configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

Install the bundle with Composer, pinning the major version:

composer require exs/php-user-agent-bundle:^1.0

Register it using the style that matches your Symfony version. The README targets Symfony2:

// app/AppKernel.php
new EXS\PhpUserAgentBundle\EXSPhpUserAgentBundle(),

For a modern Symfony app, add it to config/bundles.php if the bundle supports your framework version. Before relying on it, open vendor/exs/php-user-agent-bundle/ and inspect Services/PhpUserAgentService.php to confirm the actual public methods and service ID — the README example is old and contains a typo (getFlatform).

First use case: parse the current request’s User-Agent header and use the result for lightweight analytics, feature hinting, or logging:

$service = new \EXS\PhpUserAgentBundle\Services\PhpUserAgentService();
$service->parseUserAgent($request->headers->get('User-Agent'));

$platform = $service->getFlatform(); // verify method name in source
$browser  = $service->getBrowser();
$version  = $service->getVersion();

Implementation Patterns

Inject the service into controllers or application services. If the bundle’s service is not autowired, define an alias or explicit service in config/services.yaml. Keep your own thin adapter around it so you can replace the bundle later without touching call sites.

public function index(Request $request, PhpUserAgentService $userAgent)
{
    $userAgent->parseUserAgent($request->headers->get('User-Agent'));

    return $this->json([
        'platform' => $userAgent->getFlatform(),
        'browser'  => $userAgent->getBrowser(),
        'version'  => $userAgent->getVersion(),
    ]);
}

Parse once per request. The service appears stateful: parseUserAgent() likely stores the result internally, and the getters expose it. Parse the request header once, then read the getters. In a kernel request listener or custom request-scoped helper, store the parsed values in request attributes or a small value object rather than passing the mutable service around.

Centralize access. Create your own UserAgentInfo value object and a factory/service that wraps this bundle. That gives you typed properties, a stable API, and a single place to swap in donatj/PhpUserAgent directly if the bundle becomes incompatible.

final class UserAgentInfo
{
    public function __construct(
        public readonly string $platform,
        public readonly string $browser,
        public readonly string $version,
    ) {}
}

Use it in listeners, Twig extensions, or logging context. For a Twig helper, build a Twig extension that injects your adapter and exposes ua_platform(), ua_browser(), and ua_version(). For logging, add the parsed browser/platform to Monolog context so you can filter by client type.

Test with representative UA strings. Do not depend on your local browser alone. Include desktop Chrome, Safari on iOS, Android Chrome, a bot, and an empty string in your test cases.

Gotchas and Tips

  • Verify method names in source. The README documents getFlatform() — likely a typo for getPlatform(). Check vendor/exs/php-user-agent-bundle/Services/PhpUserAgentService.php before writing production code.

  • Symfony version compatibility is the biggest risk. This bundle is presented in Symfony2 style and has very low adoption. Check its composer.json constraints; it may not work with Symfony 5/6+. If it fails to install or autowire, use donatj/PhpUserAgent directly and wrap it in your own service.

  • The bundle is a thin wrapper around a global function. The underlying library exposes \donatj\UserAgent\parse_user_agent(). If the bundle service is awkward or unavailable, you can call that function directly and get an array with platform, browser, and version.

  • The service is likely stateful. Reusing one instance for multiple UA strings without re-parsing can return stale data. In long-running workers such as RoadRunner or Swoole, do not share the service across requests unless you reset it explicitly.

  • Always pass the UA string explicitly. If you omit it, the underlying library may fall back to $_SERVER['HTTP_USER_AGENT'], which is unreliable in CLI, queue workers, and tests.

  • Handle unknown and empty values. Unknown browsers/platforms often come back as unknown or an empty version. Treat these as normal cases and avoid strict assumptions.

  • Do not use user-agent parsing for security decisions. UA strings are trivially spoofed. Use this for analytics, UX hints, and logging only.

  • Debug by comparing raw output. If the bundle returns unexpected values, run the underlying parser directly and compare:

    var_dump(\donatj\UserAgent\parse_user_agent($request->headers->get('User-Agent')));
    
  • Find the real service ID with the container debugger. Run bin/console debug:container PhpUserAgent to see whether the bundle registered a service and what its ID is. Do not assume the class name is the service ID.

  • Pin the version and audit the dependency. With minimal adoption and a README-level maturity signal, pin a known-good version and keep your own adapter layer so you are not locked in.

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.
coleus/frame
thtg88/journalism
filagora/filagora-ai-forums
rlacerda83/query-parser
socialdept/atp-orm
cboxdk/laravel-assistant-client
socialdept/atp-resolver
socialdept/tenant-domains
tautvydasr/blade-class-names
socialdept/color-scales
maherelgamil/controller
coleus/widgets
coleus/table
coleus/users
coleus/music
coleus/health
coleus/calendar
coleus/notes
coleus/support
coleus/settings