xstreamka / yii2-mobile-detect
为 Yii2 开发的 Mobile_Detect 类,具有添加新设备的功能。
v1.1
2024-05-13 07:11 UTC
Requires
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-09-13 11:59:37 UTC
README
为 Yii2 开发的 Mobile_Detect 类,具有添加新设备的功能。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一
php composer.phar require --prefer-dist xstreamka/yii2-mobile-detect "*"
或将以下内容添加到您的 composer.json
文件的 require 部分中。
"xstreamka/yii2-mobile-detect": "*"
使用方法
安装扩展后,只需在代码中使用即可
xstreamka\mobiledetect\Device::$isPhone;
添加新设备(您的设备列表)
在配置文件(frontend/config/main.php
)中配置组件
'components' => [ ... 'device' => [ 'class' => 'xstreamka\mobiledetect\Device', 'tablet' => ['SM-T975'], // Array of users' tablets devices. 'phone' => [] // Array of users' phone devices. ], ... ]
工具
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
示例
<?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 } ?> ...