Skip to content

dungeonworx/devicedetector

Repository files navigation

Laravel DeviceDetector Bridge

Latest Stable Version Latest Unstable Version Total Downloads License Build Status

This package was created out of a need to integrate Piwik's DeviceDetector package into my Laravel application in a specific way. After working on the code in my project, I realized this package would be best suited to be a package and committed publicly for consumption.

The goal of this project is to provide easier access to the DeviceDetector library and also wrap it in an easy to use Laravel Facade to facilitate seamless access in other areas of the code. This library also provides a middleware which provides some additional information in your request payload to modify parts of your application based on the device that is currently connected.

Usage

Facade

If you include the middleware in your requests, you only need to use the Facade's methods, which are documented.

If you do not use the middleware in your request, or you wish to check a different user agent, you will need to pass the optional user_agent parameter to the specific method. Please note, this changes the parsed results for all future requests as well. So if you wish to go back to returning value for the current browser request, you will need to call DeviceDetector::get($request->userAgent()) once more to reset back to the current browsing session.

use Dungeonworx\DeviceDetector\DeviceDetector;

// Dump the DeviceDetector attributes to the client.
dd(DeviceDetector::getAttributes());
[
    "browser" => true,
    "camera" => false,
    "car" => false,
    "console" => false,
    "desktop" => true,
    "feature_phone" => false,
    "feed_reader" => false,
    "library" => false,
    "media_player" => false,
    "mobile_app" => false,
    "mobile_device" => false,
    "phone_tablet" => false,
    "pim" => false,
    "portable_media_player" => false,
    "smart_display" => false,
    "smart_phone" => false,
    "tablet" => false,
    "touch_enabled" => false,
    "tv" => false,
]

Middleware

To use this package in your middleware, just add the device_detector middleware alias to your web middleware in the Http/Kernel.php file in your project. Once that's installed, you can access the four included detectors in your request. The is key in the request contains an array with four booleans; bot, desktop, mobile, and touch. These booleans are available for every request and also cached to your already assigned cache repository.

// Get the booleans from DeviceDetector.
$request->get('is');
[
    'bot'     => false,
    'desktop' => true,
    'mobile'  => false,
    'touch'   => false,
]

Documentation

This package is documented using phpDocumentor and it's API documentation can be located here.