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

Detect Mobile Devices in PHP 7.4 to Render Different Template Files

Reference: https://github.com/serbanghita/Mobile-Detect/tree/3.74.3. I can only use the latest 3.* version because I am currently using PHP 7.4, and its 4.* version requires PHP 8 or above.

作者:

在

, ,

1. Because the frontend has separate pages for mobile and desktop devices, I need to detect whether the device is mobile in PHP 7.4, and then render different template files.

2. Reference: Mobile-Detect. I can only use the latest 3.* version because I am currently using PHP 7.4, and its 4.* version requires PHP 8 or above. See Figure 1.

Reference: https://github.com/serbanghita/Mobile-Detect/tree/3.74.3. I can only use the latest 3.* version because I am currently using PHP 7.4, and its 4.* version requires PHP 8 or above.
Figure 1

3. Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. Under Modules, plugins, ports -> Yii Framework on the page, I found that the implemented Yii2 extensions have not been updated for about 5 years and depend on “mobiledetect/mobiledetectlib”: “~2.8”. I ultimately decided to use “mobiledetect/mobiledetectlib”: “^3.74”. I configured composer.json and installed the update. See Figure 2.

Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment. Under Modules, plugins, ports -> Yii Framework on the page, I found that the implemented Yii2 extensions have not been updated for about 5 years and depend on "mobiledetect/mobiledetectlib": "~2.8". I ultimately decided to use "mobiledetect/mobiledetectlib": "^3.74". I configured composer.json and installed the update
Figure 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. Then, I implemented the following in the controller:

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  ·  邮件联系