Yii2 source code learning notes (17th), yii2 source code learning notes _ PHP Tutorial

Source: Internet
Author: User
Yii2 source code learning notes (17th) and yii2 source code learning notes. Yii2 source code learning notes (17th), yii2 source code learning notes Theme class, application topic, implement the application of the topic by replacing the path, the method is to get the root path and Root link: yii2baseT yii2 source code learning notes (17th) and yii2 source code learning Notes

Theme class: the topic of the application. you can use the replacement path to get the root path and Root link: yii2 \ base \ Theme. php.

1
 '@ App/themes/basic'] ', 32 * then the themed version for a view file' @ app/views/site/index. php 'will be 33 * '@ app/themes/basic/site/index. php '. 34*35 * It is possible to map a single path to multiple paths. for example, 36*37 *~~~ 38 * 'pathmap' => [39 * '@ app/view' => [40 *' @ app/themes/christmas ', 41 * '@ app/themes/basic', 42 *], 43 *] 44 *~~~ 45*46 * In this case, the themed version cocould be either '@ app/themes/christmas/site/index. php 'or 47 *' @ app/themes/basic/site/index. php '. the former has precedence over the latter if both files exist. 48*49 * To use a theme, you shoshould configure the [[View: theme | theme] property of the "view" application 50 * component like the following: 51*52 *~~~ 53 * 'View' => [54 * 'theme '=> [55 * 'basepath' =>' @ app/themes/basic ', 56 * 'baseurl' => '@ web/themes/basic', 57 *], 58 *], 59 *~~~ 60*61 * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder 62 * that contains the entry script of the application. if your theme is designed to handle modules, 63 * you may configure the [[pathMap] property like described above. 64*65 * @ property string $ basePath The root path of this theme. all resources of this theme are located under this 66 * directory. 67 * @ property string $ baseUrl The base URL (without ending slash) for this theme. all resources of this theme 68 * are considered to be under this base URL. this property is read-only. 69*70 * @ author Qiang Xue
 
  
71 * @ since 2.0 72 */73 class Theme extends Component 74 {75/** 76 * @ var array the mapping between view directories and their corresponding themed versions. 77 * This property is used by [[applyTo ()] when a view is trying to apply the theme. 78 * Path aliases can be used when specifying directories. 79 * replacing the 80 ing relationship with path ing property settings 80 * If this property is empty or not set, a mapping [[Application: basePa Th] to [[basePath] will be used. 81 */82 public $ pathMap; 83 84 private $ _ baseUrl; // Set the url 85 86/** 87 * @ return string the base URL (without ending slash) for this theme. all resources of this theme are considered 88 * to be under this base URL. returns the base link of the current topic. Other resources are in the link. 89 */90 public function getBaseUrl () 91 {92 return $ this-> _ baseUrl; 93} 94 95/** 96 * @ param $ url string the base URL Or path alias for this theme. all resources of this theme are considered 97 * to be under this base URL. set the basic link 98 */99 public function setBaseUrl ($ url) 100 {101 $ this-> _ baseUrl = rtrim (Yii: getAlias ($ url ),'/'); 102} 103 104 private $ _ basePath; // root path 105 106/** 107 * @ return string the root path of this theme. all resources of this theme are located under this directory.108 * get the root path of the current topic 109 * @ see pa ThMap110 */111 public function getBasePath () 112 {113 return $ this-> _ basePath; 114} 115 116/** 117 * @ param string $ path the root path or path alias of this theme. all resources of this theme are located118 * under this directory. set the root path of the current topic to 119 * @ see pathMap120 */121 public function setBasePath ($ path) 122 {123 $ this-> _ basePath = Yii: getAlias ($ path ); 124} 125 126/** 127 * Converts a file to a themed f Ile if possible. replace a file with the topic file 128 * If there is no corresponding themed file, the original file will be returned.129 * does not have the corresponding topic file, and the original file is returned. 130 * @ param string $ path the file to be themed131 * @ return string the themed file, or the original file if the themed version is not available.132 * @ throws InvalidConfigException if [[basePath] is not set133 */134 public function applyTo ($ path) 135 {136 $ pathMap = $ this-> pathMap; // Obtain the path ing 137 if (empty ($ pathMap )) {// if no value is set, an exception is thrown. 138 if ($ basePath = $ this-> getBasePath () = null) {139 throw new Inv AlidConfigException (The "basePath" property must be set. '); 140} 141 // The value is in the form of [module root path => topic root path]. 142 $ pathMap = [Yii: $ app-> getBasePath () => [$ basePath]; 143} 144 $ path = FileHelper: normalizePath ($ path "/". "\" for unified 146 147 foreach ($ pathMap as $ from =>$ tos) {148 // source 149 $ from = FileHelper: normalizePath (Yii :: getAlias ($ from )). DIRECTORY_SEPARATOR; 150 if (strpos ($ path, $ from) === 0 ){/ /If there are replaceable old values in $ path 151 $ n = strlen ($ from); 152 foreach (array) $ tos as $ to) {153 $ to = FileHelper:: normalizePath (Yii: getAlias ($ )). DIRECTORY_SEPARATOR; 154 $ file = $. substr ($ path, $ n); // replace $ from in $ path with $ to155 if (is_file ($ file) {156 return $ file; // the file directly returns 157} 158} 159} 160} 161 162 return $ path; 163} 164 165/** 166 * Converts a relative URL into an absolute URL using [[baseUrl]. 167 * convert a relative U RL to absolute URL168 * @ param string $ url the relative URL to be converted. the relative URL169 * @ return string the absolute URL to be converted replaces the absolute URL170 * @ throws InvalidConfigException if [[baseUrl] is not set171 */172 public function getUrl ($ url) 173 {174 if ($ baseUrl = $ this-> getBaseUrl ())! = Null) {// The URL exists and is converted to 175 return $ baseUrl. '/'. ltrim ($ url, '/'); 176} else {// No Exception thrown 177 throw new InvalidConfigException ('The "baseUrl" property must be set. '); 178} 179} 180 181/** 182 * Converts a relative file path into an absolute one using [[basePath]. 183 * generate absolute path 184 * @ param string $ path the relative file path to be converted. the relative file path to be converted. 185 * @ return string the absolute file path absolute path after conversion 186 * @ throws InvalidConfigException if [[baseUrl] is not set187 */188 public function getPath ($ path) 189 {190 if ($ basePath = $ this-> getBasePath ())! = Null) {191 // return the spliced path 192 return $ basePath. DIRECTORY_SEPARATOR. ltrim ($ path, '/\'); 193} else {194 throw new InvalidConfigException (The "basePath" property must be set. '); 195} 196} 197}
 

Week (17), yii2 source code learning note Theme class, the topic of the application, by replacing the path to implement the topic application, the method is to get the root path and Root link: yii2 \ base \ T...

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.