dkeeper / yii2-mobiledetect

根据请求信息检测设备类型

安装数: 36,062

依赖: 0

建议者: 0

安全: 0

星标: 7

关注者: 5

分支: 4

开放问题: 1

类型:yii2-extension

dev-master 2014-08-26 18:53 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:51:16 UTC


README

根据请求信息检测设备类型。

注意

此扩展基于mobiledetect项目 http://mobiledetect.net/

有关更多文档和示例,请参阅[此处] (https://github.com/serbanghita/Mobile-Detect/) 和[此处] (http://demo.mobiledetect.net/)。

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist dkeeper/yii2-mobiledetect "*"

或添加以下内容到您的 composer.json 文件的require部分:

"dkeeper/yii2-mobiledetect": "*"

使用方法

更新配置文件 config/web.phpcommon/main.php

return [
    ...
    'components' => [
        'mobiledetect' => [
            'class' => 'dkeeper\mobiledetect\Detect',
        ],
    ],
    ...
];

在运行应用程序之前设置事件处理程序以检测设备类型

// path/to/web/index.php
$application = new yii\web\Application($config);

Yii::$app->on(\yii\base\Application::EVENT_BEFORE_REQUEST,function($event){
    Yii::$app->params['detect'] = [
        'isMobile' => Yii::$app->mobiledetect->isMobile(),
        'isTablet' => Yii::$app->mobiledetect->isTablet(),
    ];
});

$application->run();