Yii2 Theme usage details _ php instances

Source: Internet
Author: User
This article mainly introduces the Theme usage in Yii2, and analyzes the configuration methods, functions, and related attributes of the Theme in the form of instances, for more information about Yii2 Theme usage, see the following example. We will share this with you 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 yii \ base \ Theme class. its main idea is to first define a one-to-one string ing array, then, replace the given string according to the ing relationship in the array.

There are the following mappings:

$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. the above ing relationship shows that @ app/a is replaced with @ app/aaa and the generated result is @ app/aaa/site/index. php.

But note that this is not the final result. Because Yii operates on the file path, if @ app/aaa/site/index. if the php file exists, the path is returned. Otherwise, the original path @ app/a/site/index is returned. php

If there is a string @ app/c/site/index. php, because the above ing knows that @ app/c corresponds to two replicas, Yii will be replaced in sequence from the beginning, Mr Into @ app/ccc/xxx/site/index. php. If this file exists, the path is returned; otherwise, the replacement will continue.

If no corresponding file exists in the replacement result, the original path is returned.

Writing multiple replacement targets at the same time has the following advantages: the inheritance of a topic is realized.

Now there is a set of default themes. if you want to add a set of black themes, there are two ways to achieve this.

First, copy all the views in default to the blank directory.

Type 2: copy only one layout file to the blank directory, and then modify the overall color in the layout file. Then set

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

The benefit is that if no file is found in blank, it will be searched from default. that is to say, the file in blank will overwrite the file existing in default, thus realizing the inheritance of the topic.

Attributes in the topic:

$ PathMap: this is used to set the replacement ING.

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

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

$ BaseUrl: specifies the url of the resource to be accessed ("/" is not added at the end).

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

Method in the topic:

Public function init ()

Public function init () {parent: init (); // if $ pathMap ING is not set, use $ basePath, if (empty ($ this-> pathMap )) {/** if $ basePath is not set, an exception occurs. * That is to say, $ pathMap and $ basePath must be set at least one. if both are set, $ pathMap */if ($ basePath = $ this-> getBasePath () is preferred ()) === null) {throw new InvalidConfigException ('The "basePath" property must be set. ');} // Set the ing between the current module path and $ basePath $ this-> pathMap = [Yii: $ app-> getBasePath () => [$ basePath];}

Public function applyTo ($ path)

// This is the public function applyTo ($ path) that replaces the $ path string according to the ing relationships defined in $ pathMap) {// replace "/" and "\" in the path. $ path = FileHelper: normalizePath ($ path ); foreach ($ this-> pathMap as $ from => $ tos) {// map the source (old value) in the array $ from = FileHelper: normalizePath (Yii :: getAlias ($ from )). DIRECTORY_SEPARATOR; // if there is an old value that can be replaced in $ path if (strpos ($ path, $ from) === 0) {$ n = strlen ($ from ); // for the target value loop, foreach (array) $ tos as $ to) {$ to = FileHelper: normalizePath (Yii: getAlias ($ )). DIRECTORY_SEPARATOR; // replace $ from in $ path with $ to $ file = $. substr ($ path, $ n); // if it is a file, return if (is_file ($ file) {return $ file ;}}} return $ path ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.