Home > PHP Framework > YII > body text

How to adaptively switch templates in yii2

王林
Release: 2020-02-10 11:14:45
Original
2414 people have browsed it

How to adaptively switch templates in yii2

1. Create behavior

// frontend/behaviors/MobileBehavior.php
class MobileBehavior extends \yii\base\Behavior{    public function events()
        {            return [
                    \yii\web\Controller::EVENT_BEFORE_ACTION => 'beforeAction'
                ];
        }        public function beforeAction($event)
        {            if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') {
                    $event->sender->module->setViewPath($event->sender->module->getBasePath().DIRECTORY_SEPARATOR.'H5Views');
                } 
        }
}
Copy after login

2. Create a base class BaseController.php (free learning tutorial sharing: php tutorial)

/ frontend/controllers/BaseController.php
class BaseController extends \yii\web\Controller{    public function behaviors()
        {            return [
                    \frontend\behaviors\MobileBehavior::className()
                ];
        }        // 其他action}
Copy after login

Other controllers inherit BaseController.
3. Create H5Views under frontend, and just put the H5 template file here.

Related recommendations: yii tutorial

The above is the detailed content of How to adaptively switch templates in yii2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template