Yii - 主题
主题可以帮助您将一组视图替换为另一组视图,而无需修改原始视图文件。您应该设置视图应用程序组件的 theme 属性以使用主题。
您还应该定义以下属性 −
yii\base\Theme::$basePath − 定义 CSS、JS、图像等的基本目录。
yii\base\Theme::$baseUrl − 定义主题资源的基本 URL。
yii\base\Theme::$pathMap −定义替换规则。
例如,如果您在 UserController 中调用 $this->render('create'),则将呈现 @app/views/user/create.php 视图文件。但是,如果您像以下应用程序配置中一样启用主题,则将呈现视图文件 @app/themes/basic/user/create.php。
步骤 1 − 以这种方式修改 config/web.php 文件。
<?php $params = require(__DIR__ . '/params.php'); $config = [ 'id' => 'basic', 'basePath' => dirname(__DIR__), 'bootstrap' => ['log'], 'components' => [ 'request' => [ // !!! insert a secret key in the following (if it is empty) - this //is required by cookie validation 'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO', ], 'cache' => [ 'class' => 'yii\caching\FileCache', ], 'user' => [ 'identityClass' => 'app\models\User', 'enableAutoLogin' => true, ], 'errorHandler' => [ 'errorAction' => 'site/error', ], 'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', // 默认将所有邮件发送到一个文件。您必须设置 // 'useFileTransport' 为 false 并配置传输 // 以便邮件程序发送真实的电子邮件。 'useFileTransport' => true, ], 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => 'yii\log\FileTarget', 'levels' => ['error', 'warning'], ], ], ], 'view' => [ 'theme' => [ 'basePath' => '@app/themes/basic', 'baseUrl' => '@web/themes/basic', 'pathMap' => [ '@app/views' => '@app/themes/basic', ], ], ], 'db' => require(__DIR__ . '/db.php'), ], 'modules' => [ 'hello' => [ 'class' => 'app\modules\hello\Hello', ], ], 'params' => $params, ]; if (YII_ENV_DEV) { // 针对"dev"环境的配置调整 $config['bootstrap'][] = 'debug'; $config['modules']['debug'] = [ 'class' => 'yii\debug\Module', ]; $config['bootstrap'][] = 'gii'; $config['modules']['gii'] = [ 'class' => 'yii\gii\Module', ]; } return $config; ?>
我们已添加视图应用程序组件。
步骤 2 − 现在创建 web/themes/basic 目录结构和 themes/basic/site。在 themes/basic/site 文件夹中创建一个名为 about.php 的文件,其中包含以下代码。
<?php /* @var $this yii\web\View */ use yii\helpers\Html; $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing, views, meta, tags']); $this->registerMetaTag(['name' => 'description', 'content' => 'This is the description of this page!'], 'description'); ?> <div class = "site-about"> <h1><?= Html::encode($this->title) ?></h1> <p style = "color: red;"> This is the About page. You may modify the following file to customize its content: </p> </div>
步骤 3 − 现在,转到 http://localhost:8080/index.php?r=site/about,将呈现 themes/basic/site/about.php 文件,而不是 views/site/about.php。
步骤 4 − 要主题模块,请按此方式配置 yii\base\Theme::$pathMap 属性。
'pathMap' => [ '@app/views' => '@app/themes/basic', '@app/modules' => '@app/themes/basic/modules', ],
步骤 5 − 要主题化小部件,请按此方式配置 yii\base\Theme::$pathMap 属性。
'pathMap' => [ '@app/views' => '@app/themes/basic', '@app/widgets' => '@app/themes/basic/widgets', // <-- !!! ],
有时您需要指定一个包含应用程序基本外观的基本主题。要实现此目标,您可以使用主题继承。
步骤 6 − 以这种方式修改视图应用程序组件。
'view' => [ 'theme' => [ 'basePath' => '@app/themes/basic', 'baseUrl' => '@web/themes/basic', 'pathMap' => [ '@app/views' => [ '@app/themes/christmas', '@app/themes/basic', ], ] ], ],
在上述配置中,@app/views/site/index.php 视图文件将被主题化为 @app/themes/christmas/site/index.php 或 @app/themes/basic/site/index.php,具体取决于哪个文件存在。如果两个文件都存在,则将使用第一个文件。
步骤 7 − 创建 themes/christmas/site 目录结构。
步骤 8 − 现在,在 themes/christmas/site 文件夹中,使用以下代码创建一个名为 about.php 的文件。
<?php /* @var $this yii\web\View */ use yii\helpers\Html; $this->title = 'About'; $this->params['breadcrumbs'][] = $this->title; $this->registerMetaTag(['name' => 'keywords', 'content' => 'yii, developing, views, meta, tags']); $this->registerMetaTag(['name' => 'description', 'content' => 'This is the description of this page!'], 'description'); ?> <div class = "site-about"> <h2>Christmas theme</h2> <img src = "http://pngimg.com/upload/fir_tree_PNG2514.png" alt = ""/> <p style = "color: red;"> This is the About page. You may modify the following file to customize its content: </p> </div>
步骤 9 − 如果您访问 http://localhost:8080/index.php?r=site/about,您将看到使用圣诞主题更新的关于页面。