xstreamka/yii2-mobile-detect Mobile_Detect class for Yii2 with the ability to add new devices.

Device detect for Yii2

  1. Installation
  2. Usage
  3. Add new device (your list of devices)
  4. Tools
  5. Example

Mobile_Detect class for Yii2 with the ability to add new devices.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist xstreamka/yii2-mobile-detect "*"

or add

"xstreamka/yii2-mobile-detect": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by:

xstreamka\mobiledetect\Device::$isPhone;

Add new device (your list of devices)

Configure the component in your configuration file (frontend/config/main.php):

'components' => [
    ...
    'device' => [
        'class' => 'xstreamka\mobiledetect\Device',
        'tablet' => ['SM-T975'], // Array of users' tablets devices.
        'phone' => [] // Array of users' phone devices.
    ],
    ...
]

Tools

Device::$isMobile;  // Mobile: Tablet or Phone.
Device::$isTablet;  // Tablet
Device::$isPhone;   // Phone
Device::$isIphone;  // iPhone
Device::$isSamsung; // Samsung
Device::$info;      // About device (HTTP_USER_AGENT)

// Device::$detect === Mobile_Detect()
Device::$detect->isTablet();
Device::$detect->isMobile();
...
Device::$detect->isiOS();
// more here: https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples

Example

<?php
use xstreamka\mobiledetect\Device;
...
?>
<h1>Hello World</h1>
...
<?php if (Device::$isPhone) { ?>
<p>text for phone devices</p>
<?php } elseif (Device::$isTablet) { ?>
<p>text for tablet devices</p>
<?php } else { ?>
<p>text for other devices</p>
<?php } ?>
...
0 0
1 follower
1 440 downloads
Yii Version: 2.0
License: MIT
Category: Others
Developed by: xstreamka
Created on: Nov 2, 2020
Last updated: 10 months ago
Packagist Profile
Github Repository

Related Extensions