soumen-dey/laravel-user-agent

An advanced user agent detector for Laravel 5.5 and above.

v1.0 2018-07-08 15:14 UTC

This package is not auto-updated.

Last update: 2025-02-19 05:41:01 UTC


README

An advanced user agent detector for Laravel 5.5 and above.

A powerful package allowing you to detect the user's Browser, Platform and more. This package provides a very simple API and requires no configurations at all.

Installation

Via Composer

$ composer require soumen-dey/laravel-user-agent

Note: This package is tested and is fully compatible with Laravel 5.5 and above, not sure about support below Laravel 5.5.

If you are on Laravel 5.4 or below, you need to manually register the ServiceProvider and the Alias. In your config/app.php file just do:

'providers' => [
    // ...
    Soumen\Agent\AgentServiceProvider::class,
];

'aliases' => [
	//...
    'Agent' => Soumen\Agent\Facades\Agent::class,
];

For Laravel 5.5 and above, you don't need to do anything, just install the package and you are good to go!

Usage

Setup

If you have an alias set up for the Agent class, you don't need to import anything. However, if the alias is not set, you can do:

use Soumen\Agent\Agent;

// or

use Soumen\Agent\Facades\Agent

You are now ready to use the package.

API

The Agent Facade

You can access the visitor's details such as the browser using:

Agent::browser(); // returns all the details related to the visitor's browser

Likewise you can also fetch other details:

Agent::ip(); // the ip address of the visitor
Agent::device(); // details related to the visitor's device, eg, Phone, Tablet, etc.
Agent::header(); // details related to the request's header
Agent::platform(); // details related to the platform of the visitor, eg. Windows, etc.
Agent::userAgent(); // returns the HTTP_USER_AGENT string

You can also fetch server related info:

Agent::server(); // returns details related to the server

Available Methods

You can also retrieve properties:

Agent::browser('name'); // Firefox 61
Agent::browser()->name; // Firefox 61
Agent::browser()->getName(); // Firefox 61

Individual services handles the task of fetching visitor information, for more information read the Services section.

To get individual service:

$browser = Agent::get('browser'); // returns the browser service
$browser = Agent::browser(); // same as above

Services

The Agnet facade is a global wrapper for individual services that handles the actual task of fetching information about the visitor. You can call the Services directly if you want to be more specific.

Get details about the visitor's browser for example:

use Soumen\Agent\Services\Browser;

Browser::get(); // returns all details related to the visitor's browser
Browser::getDetails(); // same as get() method

The API for every service is similar except for some minor changes, so you can do:

use Soumen\Agnet\Services\Device;
use Soumen\Agent\Services\Platform;

$device = Device::get(); // get details about the visitor's device
$platform = Platform::get(); // get details related to the visitor's platform

Available Services

Fetching Properties

To get individual browser property such as name do one of the following:

$browser = Agent::browser('name');
$browser = Agent::browser()->name;
$browser = Agent::browser()->name();
$browser = Agent::browser()->getName();

Using the Service:

use Soumen\Agent\Services\Browser;

// get the name of the browser
$browser = Browser::get('name');
$browser = Browser::get()->name;
$browser = Browser::get()->name();
$browser = Browser::get()->getName();

// or

$browser = Browser::getDetails('name');
$browser = Browser::getDetails()->name;
...
// You get the idea

This applies to all the available properties of the browser. Refer to the section for each service for more information.

Properties as parameter

You can use the property name as the parameter, so you can do:

Agent::browser('name'); // Firefox 61
Agent::platform('name'); // Windows 10

Instead of:

Agent::browser()->name;
Agent::platform()->name;

Using individual service:

use Soumen\Agent\Services\Browser;
use Soumen\Agent\Services\Platform;

Browser::get('name'); // Firefox 61
Platform::get('name'); // Windows 10

// or

Browser::getDetails('name'); // Firefox 61
Platform::getDetails('name'); // Windows 10

Note: The parameter should be the exact same string as the property name.

For a list of available properties, refer to the Properties section of the respective Service

Browser

Using the Facade:

$browser = Agent::browser();

Using the Service:

use Soumen\Agent\Services\Browser;

$browser = Browser::get();

// or

$browser = Browser::getDetails();
Available Properties:

Platform

Using the Facade:

$platform = Agent::platform();

Using the Service:

use Soumen\Agent\Services\Platform;

$platform = Platform::get();

// or

$platform = Platform::getDetails();
Available Properties:

Device

Using the Facade:

$device = Agent::device();

Using the Service:

use Soumen\Agent\Services\Device;

$device = Device::get();

// or

$device = Device::getDetails();
Available Properties:

User Agent

Using the Facade:

$userAgent = Agent::userAgent();

Using the Service:

use Soumen\Agent\Services\UserAgent;

$userAgent = UserAgent::get();

No specific properties for this service.

Header

Using the Facade:

$header = Agent::header();

Using the Service:

use Soumen\Agent\Service\Header;

$header = Header::get();

// or

$header = Header::getDetails();

Available Properties

You can use the property names as the string in the parameter column:

Header::get('user-agent');
Agent::header('user-agent');

Server

Using the Facade:

$server = Agent::server();

Using the Service:

use Soumen\Agent\Services\Server;

$server = Server::get();

// or

$server = Server::getDetails();

Available Properties

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email me at soumendeyemail@gmail.com.

Credits

License

This package is released under the MIT License (MIT). Please see the license file for more information.