Laravel PHP · Inline✓ Human-reviewed

App-owned browser and OS labels in Laravel PHP

Remove Agent's Laravel wrapper for display-only browser/platform consumers while retaining a maintained parser, nullable labels and long-lived-worker isolation.

Approved 2026-12-04T13:20:39.919659Z · Submission 50

App-owned browser and OS labels in Laravel PHP

Scope and attribution

Remove the Agent Laravel wrapper only where callers need informational browser and operating-system labels. Retain a maintained parsing library rather than copying a mobile-detection database into the app. The package and framework refs this recipe was checked against are in its metadata; reconcile them with composer.lock before implementation.

Dependency source checked on 2026-12-04: ua-parser/uap-php, release 3.10.0, commit f44bdd1b38198801cf60b0681d2d842980e47af5. Inspect src/Parser.php, src/ParserFactoryMethods.php and the shipped rule data. This is a research baseline, not an instruction to upgrade blindly.

Jens Segers' Agent is MIT licensed. Inspect src/Agent.php, src/AgentServiceProvider.php, tests/AgentTest.php and composer.json; preserve attribution if copying source or fixtures. This alternative uses ua-parser/uap-php, whose library is MIT licensed and whose ua-parser data has Apache licensing; preserve the shipped notices for both. No destination application has been converted or tested with this guidance.

Source behavior and compatibility boundary

If your Agent uses the inspected implementation, it extends Mobile Detect, adds ordered browser/platform expressions, and delegates crawler detection separately. Specific Edge and Opera tokens can take precedence over Chrome. Unmatched browser/platform methods return false. Its Laravel provider registers an Agent singleton holding request server data.

ua-parser is a replacement engine, not an Agent-compatible parser. Labels such as versioned Windows names differ. This recipe intentionally exposes nullable browser/platform labels and permits reviewed display-label differences. It does not cover mobile/tablet/desktop flags, robot detection, exact version values, languages(), dynamic isX calls, Blade directives or CloudFront detection. Do not remove the package while any of those are still used.

Inline the application projection

  1. Inventory all Agent calls, directives, container bindings and persisted labels. Decide whether the app can accept a different display vocabulary. Leave old analytics identifiers intact, or introduce an explicit new label version rather than silently relabeling historical records.
  2. Install the researched ua-parser release or a reconciled compatible release, committing composer.lock. Keep the shipped rule data immutable between deployments. Do not run its rule-update command in HTTP requests or let users choose a custom regex file.
  3. Read $request->userAgent(). Missing, whitespace-only and over-4096-byte values return both labels as null before invoking the parser. This cap is local policy, not Agent behavior. With the inspected parser API, the inner projection is:
$result = \UAParser\Parser::create()->parse($ua);
$labels = [
    'browser' => in_array($result->ua->family, ['', 'Other'], true)
        ? null : $result->ua->family,
    'platform' => in_array($result->os->family, ['', 'Other'], true)
        ? null : $result->os->family,
];

$ua must already have passed the checks. Return the array through the existing controller/response. If parser construction is expensive, reuse only a parser whose installed implementation does not retain request-specific input. Never cache the result as a singleton. Under Octane/long-lived workers, test sequential and overlapping requests explicitly.

  1. Replace only browser/platform consumers with these nullable labels. Do not translate unknown into desktop, non-bot or trusted device. User-Agent can be forged and cannot authorize access or prove feature support.
  2. Use Blade's escaped {{ }} output or JSON responses, not raw HTML. Keep raw headers out of ordinary logs; sanitize and limit any diagnostic sampling. Initially use private/non-shared caching for content varying by this summary; review cache variants before enabling CDN caching.
  3. After searching all app callers again, remove jenssegers/agent, its explicit provider/alias registrations and obsolete package-specific configuration. Do not remove Mobile Detect or crawler dependencies needed by other packages; use Composer's dependency graph to decide.

Trial checklist

Build licensed regression fixtures from Agent's browser/platform tests and actual app cases. Include Edge with both Chrome/ and Edg/, Opera with OPR/, Safari, iPhone, Android, a crawler and an unknown UA. Assert the agreed new labels, not assumed cross-engine equality. Test any output mappings independently of parser internals.

  • Blank, missing, arbitrary and over-limit headers return null fields; check both sides of the byte limit.
  • Sequential long-lived-worker requests and concurrent clients cannot reuse another request's result.
  • HTML-looking input is rendered as escaped text. Spoofing a browser cannot bypass authentication or any feature-access policy.
  • Existing views handle null labels. No remaining Agent facade/directive/injection call is left after removal.
  • composer validate --strict, composer audit and the application's test suite pass with the new lockfile; run its configured static analysis too.

Record PHP/Laravel/parser and rules versions, concrete fixture outputs, commands and accepted differences with the change.