Home >Backend Development >PHP Tutorial >Detailed explanation of Yii2 theme (Theme) usage_php example

Detailed explanation of Yii2 theme (Theme) usage_php example

WBOY
WBOYOriginal
2016-08-04 08:56:431041browse

The example in this article describes the usage of Yii2 theme (Theme). Share it with everyone for your reference, the details are as follows:

First, let’s take a look at the main configuration methods:

'components' => [
  'view' => [
    'theme' => [
      'pathMap' => ['@app/views' => '@app/themes/basic'],
      'baseUrl' => '@web/themes/basic',
    ],
  ],
],

The theme function in Yii is mainly implemented by the yiibaseTheme class. Its main idea is to first define a one-to-one corresponding string mapping array, and then replace the given string according to the mapping relationship in the array.

has the following mapping:

$pathMap=[
    '@app/a' => '@app/aaa',
    '@app/b' => '@app/bbb',
    '@app/c' => [
        '@app/ccc/xxx',
        '@app/ccc/yyy',
      ],
];

For the string @app/a/site/index.php, from the above mapping relationship, it can be seen that @app/a will be replaced by @app/aaa, and the generated result will be @app/aaa/site/index.php.

But please note that this is not the final result yet. Since Yii operates on file paths, if the file @app/aaa/site/index.php exists, this path will be returned, otherwise the original path will be returned: @app/a/site/index.php

If there is a string @app/c/site/index.php, since the above mapping knows that @app/c corresponds to 2 replacements, Yii will replace it sequentially from the beginning, first generating @app/ccc/xxx/site/index .php, if this file exists, return this path, otherwise continue to replace.

If all replacement results do not have corresponding files, then the original path will be returned.

Writing multiple replacement target values ​​at the same time has the advantage of realizing theme inheritance.

There is now a set of default themes. If you want to add a black theme now, there are two ways to achieve it.

The first method: Copy all the views in the default to the blank directory.

Second option: Copy only one layout file to the blank directory, and then modify the overall color in the layout file. Then set to

$pathMap=[
    '@app/c' => [
        '@app/ccc/blank',
        '@app/ccc/default',
      ],
];

See the benefits? If a file is not found in blank, it will be searched from default. That is to say, the files in blank will overwrite the files that exist in default, thus realizing the inheritance of the theme.

Properties in the theme:

$pathMap: This is used to set the replacement mapping relationship.

'pathMap' =>[
    '@app/views' => [
        '@app/themes/blank',
        '@app/themes/default',
    ],
    '@app/modules' => '@app/themes/default/modules',
    '@app/widgets' => '@app/themes/default/widgets'
],

These three apply themes to views, modules and widgets respectively.

$baseUrl: This is used to set the URL of the resource to be accessed (without "/" at the end)

$basePath: Set the file directory where the resource is located

Methods in theme:

public function init()

public function init()
{
    parent::init();
    //如果没有设置$pathMap映射,则使用$basePath,
    if (empty($this->pathMap)) {
        /*
         * 如果$basePath也没有设置,则出异常。
         * 也就是说 $pathMap和$basePath至少要设置一个,如果两个都设置了,优先使用$pathMap
         */
      if (($basePath = $this->getBasePath()) === null) {
        throw new InvalidConfigException('The "basePath" property must be set.');
      }
      //设置当前模块的路径和$basePath的映射关系
      $this->pathMap = [Yii::$app->getBasePath() => [$basePath]];
    }
}

public function applyTo($path)

//这个就是按照 $pathMap中的定义的映射关系来对$path进行替换字符串的
public function applyTo($path)
{
    //对路径中的"/"、“\”进行统一替换
    $path = FileHelper::normalizePath($path);
    foreach ($this->pathMap as $from => $tos) {
       //映射数组中的来源(旧值)
      $from = FileHelper::normalizePath(Yii::getAlias($from)) . DIRECTORY_SEPARATOR;
      //如果在$path中有可替换的旧值
      if (strpos($path, $from) === 0) {
        $n = strlen($from);
        //对目标值循环,
        foreach ((array) $tos as $to) {
          $to = FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
          //把$path中的$from替换为$to
          $file = $to . substr($path, $n);
          //如果是文件,直接返回
          if (is_file($file)) {
            return $file;
          }
        }
      }
    }
    return $path;
}

Readers who are interested in more Yii-related content can check out the special topics on this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent PHP Development Framework", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

Statement:
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