chronon/mobile_detect
Composer 安装命令:
composer require chronon/mobile_detect
包简介
A CakePHP plugin component for identifying mobile devices using the Mobile_Detect project.
README 文档
README
CakePHP's built-in CakeRequest object can easily determine if a request is from a mobile device:
$this->request->is('mobile');
However, sometimes an application needs finer control over what to serve certain devices, such as a mobile layout to smartphones and a desktop layout to tablets. The MobileDetect project is a "lightweight PHP class for detecting mobile devices". This component, packaged as a plugin, makes MobileDetect available in a CakePHP controller.
Compatibility:
Tested with CakePHP 2.10.x, but should work fine with any CakePHP 2.x version.
NOTE: This plugin is not CakePHP 3.x compatible, but the Mobile_Detect lib is included with the CakePHP 3 app skeleton and can be used and extended as needed making this plugin unnecessary.
Installation:
In your project composer.json file:
{
"require": {
"chronon/mobile_detect": "*"
},
"config": {
"vendor-dir": "Vendor"
}
}
This will install the plugin into Plugin/MobileDetect, and install the Mobile_Detect lib
(from Packagist) into your Vendor directory.
In your app's Config/bootstrap.php, import composer's autoload file:
<?php App::import('Vendor', array('file' => 'autoload'));
Using git:
You will need the component (packaged as a plugin), and the MobileDetect PHP library (not included). The MobileDetect library needs to be in this plugin's Vendor directory and must be named 'MobileDetect'. Using git, something like this:
git clone git@github.com:chronon/CakePHP-MobileDetectComponent-Plugin.git app/Plugin/MobileDetect git clone git@github.com:serbanghita/Mobile-Detect.git app/Plugin/MobileDetect/Vendor/MobileDetect
Usage:
The component has only one method, named detect. It accepts two arguments: the method to pass to
the MobileDetect library, and any arguments for the passed method.
Example: check if a request is from a tablet:
$result = $this->MobileDetect->detect('isTablet');
Example: check if a request is from an iOS device :
$result = $this->MobileDetect->detect('isiOS');
Example: get version number of an Android device:
$result = $this->MobileDetect->detect('version', 'Android');
See the demo at mobiledetect.net for a list of all available methods.
Example:
Let's say we want to serve a mobile layout to smartphones and a desktop layout to
tablets. Instead of loading the component on every request (by adding it to your controller's
$components array), we'll load the component on the fly when needed. This example sets a session
variable tablet if the request is from a tablet, calling the component only once.
In Controller/AppController.php:
<?php public function beforeFilter() { // check if the request is 'mobile', includes phones, tablets, etc. if ($this->request->is('mobile')) { if (!$this->_isTablet()) { // if the request is mobile, but not a tablet, activate the mobile layout $this->_setMobile(); } } } protected function _setMobile() { $this->theme = 'Mobile'; // etc... } protected function _isTablet() { if ($this->Session->check('tablet')) { return $this->Session->read('tablet'); } // load the component $this->MobileDetect = $this->Components->load('MobileDetect.MobileDetect'); // pass the component the 'isTablet' method $result = $this->MobileDetect->detect('isTablet'); $this->Session->write('tablet', $result); return $result; }
chronon/mobile_detect 适用场景与选型建议
chronon/mobile_detect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 256.61k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2013 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「cakephp」 「mobile」 「component」 「mobile detect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chronon/mobile_detect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chronon/mobile_detect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chronon/mobile_detect 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
Beyonic PHP Library
The skeleton application for NativePHP for Mobile.
Email Toolkit Plugin for CakePHP
PHP SDK for Mobile Message SMS API
Rhino, a CMS and Application-Framework plugin for CakePHP
统计信息
- 总下载量: 256.61k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 24
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-04-21