devicedetect / devicedetectionlib
This is a device detection-identification library
v1.0.5
2019-10-07 19:58 UTC
Requires
- php: ^7.2
- mobiledetect/mobiledetectlib: ^2.8
Requires (Dev)
- mockery/mockery: ^1.0
- nunomaduro/collision: ^3.0
- phpunit/phpunit: ^8.0
README
About
Device Detect is a lightweight PHP class for detecting devices. It is based on mobiledetect library. It uses the User-Agent string combined with specific HTTP headers to identify devices (browser, os etc).
Install
As a composer package
composer require devicedetect/devicedetectionlib
or include the dependency in the composer.json
file:
{ "require": { "devicedetect/devicedetectionlib": "1.0.*" } }
Usage
<?php /** * Retrieve the User-Agent. * @method getUserAgent() * @return string|null The user agent if it's set. */ /** * Retrieve the list of known browsers. * @method getBrowsers() * @return array */ /** * Retrieve the list of operating systems. * @method getOperatingSystems() * @return array */ /** * Retrieve the list of device types. * @method getDeviceTypes() * @return array */ /** * Get device type. * @method getDeviceType($userAgent = null) * @param null $userAgent * @return string */ /** * Get device browser. * @method getBrowser() * @return string */ /** * Retrieve the User-Agent. * @method getOperatingSystem() * @return string */ /** * Mobile device detection. * @method isMobile($userAgent = null) * @param null $userAgent * @return bool */ /** * Tablet detection. * @method isTablet($userAgent = null) * @param null $userAgent * @return bool */ /** * Computer detection * @method isComputer($userAgent = null) * @param null $userAgent * @return bool */ /** * Ipad detection * @method isIpad() * @return bool */ /** * Iphone detection * @method isIphone() * @return bool */ /** * Android Os detection * @method isAndroid() * @return bool */ /** * Get version of os, browser etc * @method getVersion(string $key) * @param string $key * @return float|string */
Code example
Inject library into your code. Use as needed.
<?php namespace App\Http\Controllers; use App\Device_Detect; /** * Class DeviceDetectionController * * @package App\Http\Controllers */ class DeviceDetectionController extends Controller { /** * @var Device_Detect */ private $deviceDetect; /** * DeviceDetectionController constructor. * * @param $deviceDetect */ public function __construct(Device_Detect $deviceDetect) { $this->deviceDetect = $deviceDetect; } /** * Device Detection */ public function detect() { print_r($this->deviceDetect->getBrowser()); echo "\n"; print_r($this->deviceDetect->getOperatingSystem()); echo "\n"; print_r($this->deviceDetect->getDeviceType()); } }