jenssegers/agent

repository·master·Indexed 26 days ago

https://github.com/jenssegers/agent

A PHP user agent parser providing desktop and mobile detection. It extends Mobile Detect to include desktop support and metadata extraction for browser/platform versions, device types (phone, tablet, desktop), and accepted languages. Includes integration for Laravel via a Service Provider and Facade, and utilizes jaybizzle/crawler-detect for robot and crawler identification.

Tokens
1.7K
Snippets
6
Records
18
Agent score
89%

What's inside jenssegers/agent

  1. Register Agent in Laravel

    master
    To use the Agent component within a Laravel application, register the Jenssegers\Agent\AgentServiceProvider in your config/app.php providers array. The package is registered as a singleton under the key agent and is aliased to the Jenssegers\Agent\Agent class. It is a deferred provider, meaning it will only be loaded when the agent service is actually requested.
  2. Check user agent properties with is() and magic methods

    master

    You can check for specific properties (OS, Browser, Device) using the is() method or via magic is{Property}() methods.

    Using is():

    $agent->is('Windows');
    $agent->is('Firefox');
    $agent->is('iPhone');
    $agent->is('OS X');

    Using magic methods:

    $agent->isAndroidOS();
    $agent->isNexus();
    $agent->isSafari();
  3. Get browser or platform version

    master

    Retrieve the version number for a specific browser or platform using the version() method. Note that this method is currently in beta and may not always return accurate results.

    $browser = $agent->browser();
    $version = $agent->version($browser);
    
    $platform = $agent->platform();
    $version = $agent->version($platform);
  4. Detect device types (Mobile, Tablet, Desktop, Phone)

    master

    Use the following methods to identify the device category:

    • $agent->isMobile(): Returns true if the device is mobile.
    • $agent->isTablet(): Returns true if the device is a tablet.
    • $agent->isDesktop(): Returns true if the user is not using a mobile device, tablet, or robot.
    • $agent->isPhone(): Returns true if the user is using a phone device.
  5. Parse custom User Agents and HTTP Headers

    master

    In environments like CLI scripts where the current request is not available, use setUserAgent() and setHttpHeaders() to parse specific user agent strings and headers.

    $agent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
    $agent->setHttpHeaders($headers);
  6. Get device, platform, browser, and robot information

    master

    Extract specific metadata from the user agent:

    • $agent->device(): Returns the device name (e.g., iPhone, Nexus, AsusTablet) if mobile.
    • $agent->platform(): Returns the operating system name (e.g., Ubuntu, Windows, OS X).
    • $agent->browser(): Returns the browser name (e.g., Chrome, IE, Safari, Firefox).
    • $agent->robot(): Returns the name of the detected robot.
    • $agent->languages(): Returns an array of the browser's accepted languages.
  7. Detect crawlers and robots

    master

    The Agent class integrates with CrawlerDetect to identify search engine crawlers and other bots.

    • isRobot([$userAgent]): Returns true if the User Agent is a known crawler.
    • robot([$userAgent]): Returns the name of the detected robot (e.g., 'Googlebot') if it is a crawler, otherwise returns false.
    • getCrawlerDetect(): Returns the underlying CrawlerDetect instance.