定制 superbig/craft3-mobiledetect 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

superbig/craft3-mobiledetect

Composer 安装命令:

composer require superbig/craft3-mobiledetect

包简介

Use Mobile_Detect for detecting mobile devices (including tablets)

README 文档

README

Use Mobile_Detect for detecting mobile devices (including tablets).

Screenshot

Requirements

  • Craft CMS 5.5.0 or later
  • PHP 8.2 or later

Installation

  1. Open your terminal and go to your Craft project:

     cd /path/to/project
    
  2. Tell Composer to load the plugin:

     composer require superbig/craft3-mobiledetect
    
  3. In the Control Panel, go to Settings → Plugins and click the "Install" button for MobileDetect.

Overview

A wrapper for the Mobile_Detect library (v4) by @serbanghita.

Usage

Twig

{# Device detection #}
{{ craft.mobileDetect.isMobile ? 'I am mobile.' : 'I am not mobile.' }}
{{ craft.mobileDetect.isTablet ? 'Tablet' : 'Not a tablet' }}
{{ craft.mobileDetect.isPhone ? 'Phone' : 'Not a phone' }}

{# OS detection — magic methods #}
{% if craft.mobileDetect.isiOS %}
    <p>Welcome, iOS user!</p>
{% elseif craft.mobileDetect.isAndroidOS %}
    <p>Welcome, Android user!</p>
{% endif %}

{# Browser detection — magic methods #}
{% if craft.mobileDetect.isChrome %}
    <p>You're using Chrome</p>
{% elseif craft.mobileDetect.isSafari %}
    <p>You're using Safari</p>
{% endif %}

{# Generic rule matching #}
{{ craft.mobileDetect.is('iOS') }}
{{ craft.mobileDetect.is('WindowsPhoneOS') }}
{{ craft.mobileDetect.is('Firefox') }}

PHP

$service = \superbig\mobiledetect\MobileDetect::getInstance()->mobileDetectService;
$isMobile = $service->isMobile();
$isIOS = $service->is('iOS');

Methods

Device detection

Method Description
isMobile Returns true for any mobile device (including tablets)
isTablet Returns true for tablets only
isPhone Returns true for phones only (mobile but not tablet)

OS & browser detection (magic methods)

Any is* call is forwarded to the library's rule matching. These work in both Twig and PHP:

Method Description
isiOS iOS devices
isAndroidOS Android devices
isBlackBerryOS BlackBerry devices
isWindowsMobileOS Windows Mobile devices
isWindowsPhoneOS Windows Phone devices
isChrome Chrome browser
isSafari Safari browser
isFirefox Firefox browser
is* Any rule the library supports — see Mobile_Detect docs

Rule-based detection

Method Description
is(key) Test any rule by name, e.g. is('iOS'), is('AndroidOS'), is('Chrome')
match(pattern) Test using a regular expression against the user agent
version(component) Get the version of a component, e.g. version('Android')

Utility methods

Method Description
getVersion Returns the Mobile_Detect library version
getUserAgent Get the current user agent string

Upgrading from v2 (Craft 4)

Breaking changes

  • mobileGrade — Removed (not available in Mobile_Detect v4)
  • getScriptVersion — Renamed to getVersion
  • getCfHeaders / setCfHeaders — Replaced by getCloudFrontHeaders (service only)
  • setUserAgent / setHttpHeaders — Available on the service only, not the Twig variable

What still works

All is* magic methods (isiOS, isAndroidOS, isChrome, etc.) continue to work in Twig templates — no changes needed.

PHP service access

The static $plugin property has been removed. Use getInstance():

// Before (Craft 4)
MobileDetect::$plugin->mobileDetectService->isMobile();

// After (Craft 5)
MobileDetect::getInstance()->mobileDetectService->isMobile();

Brought to you by Superbig

统计信息

  • 总下载量: 53.61k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 19
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 19
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2017-11-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固