没有不值得去解决的问题,也没有不值得去学习的技术!

在 PHP 7.4 中检测是否是移动设备,进而渲染不同的模板文件

参考:https://github.com/serbanghita/Mobile-Detect/tree/3.74.3 。只能够使用 3.* 的最新版本,因为现在是使用的 PHP 7.4,其 4.* 的版本要求 PHP 8 以上的版本。

作者:

在

, ,

1、由于前端,同一个页面,针对移动设备与桌面设备,是分别编写的页面。因此,需要在 PHP 7.4 中检测是否是移动设备,进而渲染不同的模板文件

2、参考:Mobile-Detect 。只能够使用 3.* 的最新版本,因为现在是使用的 PHP 7.4,其 4.* 的版本要求 PHP 8 以上的版本。。如图1

参考:https://github.com/serbanghita/Mobile-Detect/tree/3.74.3 。只能够使用 3.* 的最新版本,因为现在是使用的 PHP 7.4,其 4.* 的版本要求 PHP 8 以上的版本。
图1

3、Mobile Detect 是一个轻量级的 PHP 类,用于检测移动设备(包括平板电脑)。它使用 User-Agent 字符串结合特定的 HTTP 标头来检测移动环境。在页面的 Modules, plugins, ports -> Yii Framework,发现实现的 Yii2 扩展,都有 5 年左右没有更新,且依赖 “mobiledetect/mobiledetectlib”: “~2.8″。最终决定使用 “mobiledetect/mobiledetectlib”: “^3.74” 。配置 composer.json 。安装更新。如图2

Mobile Detect 是一个轻量级的 PHP 类,用于检测移动设备(包括平板电脑)。它使用 User-Agent 字符串结合特定的 HTTP 标头来检测移动环境。在页面的 Modules, plugins, ports -> Yii Framework,发现实现的 Yii2 扩展,都有 5 年左右没有更新,且依赖 "mobiledetect/mobiledetectlib": "~2.8"。最终决定使用 "mobiledetect/mobiledetectlib": "^3.74" 。配置 composer.json 。安装更新
图2
JSON
{
    "require": {
        "mobiledetect/mobiledetectlib": "^3.74"
    }
}

Plaintext
PS C:\wwwroot\object\src> composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. It is recommended that you run `composer update` or `composer update `.
- Required package "mobiledetect/mobiledetectlib" is not present in the lock file.
This usually happens when composer files are incorrectly merged or the composer.json file is manually edited.
Read more about correctly resolving merge conflicts https://getcomposer.org/doc/articles/resolving-merge-conflicts.md
and prefer using the "require" command over editing the composer.json file directly https://getcomposer.org/doc/03-cli.md#require-r
PS C:\wwwroot\object\src> composer update mobiledetect/mobiledetectlib
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking mobiledetect/mobiledetectlib (3.74.3)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading mobiledetect/mobiledetectlib (3.74.3)
  - Installing mobiledetect/mobiledetectlib (3.74.3): Extracting archive
Package codeception/phpunit-wrapper is abandoned, you should avoid using it. No replacement was suggested.
Package sebastian/resource-operations is abandoned, you should avoid using it. No replacement was suggested.
Generating autoload files
59 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
PS C:\wwwroot\object\src>

4、然后在控制器中实现如下

Plaintext

use Detection\MobileDetect;

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $detect = new MobileDetect();

        // $deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
        if ($detect->isMobile()) {
            return $this->render('index_mobile');
        }

        return $this->render('index');
    }

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系