Detect whether it is a mobile device in PHP 7.4, and then render different template files
1. Due to the front-end, the same page, for mobile devices and desktop devices, is a separate page. Therefore, you need to detect whether it is a mobile device in PHP 7.4, which in turn renders different template files
2. Reference:mobile-detect . Only the latest version of 3.* can be used, because PHP 7.4 is now used, and its 4.* version requires versions above PHP 8. . as shown in Figure 1
3. Mobile Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the user-agent string combined with a specific HTTP header to detect mobile environments. In the modules, plugins, ports -> yii framework of the page, I found that the implemented yii2 extensions have not been updated for about 5 years, and depend on them. “MobileDetect/MobileDetectLib”: “~2.8”. The final decision is to use “mobiledetect/mobiledetectlib”: “^3.74” . Configure composer.json . Install updates. as shown in Figure 2
{
"require": {
"mobiledetect/mobiledetectlib": "^3.74"
}
}
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 implement the following in the controller
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');
}

