国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial Android source code learning yii2 source code learning notes 17)

Android source code learning yii2 source code learning notes 17)

Jul 28, 2016 am 08:30 AM

Theme 類(lèi),應(yīng)用的主題,通過(guò)替換路徑實(shí)現(xiàn)主題的應(yīng)用,方法為獲取根路徑和根鏈接:yii2\base\Theme.php

<span>  1</span> <?<span>php
</span><span>  2</span><span>/*</span><span>*
</span><span>  3</span><span> * @link </span><span>http://www.yiiframework.com/</span><span>  4</span><span> * @copyright Copyright (c) 2008 Yii Software LLC
</span><span>  5</span><span> * @license </span><span>http://www.yiiframework.com/license/</span><span>  6</span><span>*/</span><span>  7</span><span>  8</span><span>namespace</span> yii\<span>base</span><span>;
</span><span>  9</span><span> 10</span><span>use Yii;
</span><span> 11</span><span>use yii\helpers\FileHelper;
</span><span> 12</span><span> 13</span><span>/*</span><span>*
</span><span> 14</span><span> * Theme represents an application theme.
</span><span> 15</span><span> * Theme 類(lèi),應(yīng)用的主題
</span><span> 16</span><span> * When [[View]] renders a view file, it will check the [[View::theme|active theme]]
</span><span> 17</span><span> * to see if there is a themed version of the view file exists. If so, the themed version will be rendered instead.
</span><span> 18</span><span> * 視圖對(duì)象[[View]]渲染視圖文件的時(shí)候,會(huì)檢查視圖的主題是否存在,如果存在則渲染主題取代默認(rèn)樣式
</span><span> 19</span><span> * A theme is a directory consisting of view files which are meant to replace their non-themed counterparts.
</span><span> 20</span><span> *
</span><span> 21</span><span> * Theme uses [[pathMap]] to achieve the view file replacement:
</span><span> 22</span><span> *
</span><span> 23</span><span> * 1. It first looks for a key in [[pathMap]] that is a substring of the given view file path;
</span><span> 24</span><span> *  首先查找關(guān)鍵字,關(guān)鍵字是一個(gè)給定的視圖路徑的字符串
</span><span> 25</span><span> * 2. If such a key exists, the corresponding value will be used to replace the corresponding part
</span><span> 26</span><span> *    in the view file path;關(guān)鍵字存在,則用對(duì)應(yīng)值替換給定的視圖文件路徑中對(duì)應(yīng)的部分
</span><span> 27</span><span> * 3. It will then check if the updated view file exists or not. If so, that file will be used
</span><span> 28</span><span> *    to replace the original view file.檢查替換后的路徑對(duì)應(yīng)的文件是否存在,存在就替換原文件
</span><span> 29</span><span> * 4. If Step 2 or 3 fails, the original view file will be used.
</span><span> 30</span><span> * 2和3失敗的話(huà),返回原來(lái)的路徑
</span><span> 31</span><span> * For example, if [[pathMap]] is `['@app/views' => '@app/themes/basic']`,
<span> 32</span><span> * then the themed version for a view file `@app/views/site/index.php` will be
</span><span> 33</span><span> * `@app/themes/basic/site/index.php`.
</span><span> 34</span><span> *
</span><span> 35</span><span> * It is possible to map a single path to multiple paths. For example,
</span><span> 36</span><span> *
</span><span> 37</span><span> * ~~~
</span><span> 38</span><span> * 'pathMap' => [
</span><span> 39</span><span> *     '@app/views' => [
</span><span> 40</span><span> *         '@app/themes/christmas',
</span><span> 41</span><span> *         '@app/themes/basic',
</span><span> 42</span><span> *     ],
</span><span> 43</span><span> * ]
</span><span> 44</span><span> * ~~~
</span><span> 45</span><span> *
</span><span> 46</span><span> * In this case, the themed version could be either `@app/themes/christmas/site/index.php` or
</span><span> 47</span><span> * `@app/themes/basic/site/index.php`. The former has precedence over the latter if both files exist.
</span><span> 48</span><span> *
</span><span> 49</span><span> * To use a theme, you should configure the [[View::theme|theme]] property of the "view" application
</span><span> 50</span><span> * component like the following:
</span><span> 51</span><span> *
</span><span> 52</span><span> * ~~~
</span><span> 53</span><span> * 'view' => [
</span><span> 54</span><span> *     'theme' => [
</span><span> 55</span><span> *         'basePath' => '@app/themes/basic',
</span><span> 56</span><span> *         'baseUrl' => '@web/themes/basic',
</span><span> 57</span><span> *     ],
</span><span> 58</span><span> * ],
</span><span> 59</span><span> * ~~~
</span><span> 60</span><span> *
</span><span> 61</span><span> * The above configuration specifies a theme located under the "themes/basic" directory of the Web folder
</span><span> 62</span><span> * that contains the entry script of the application. If your theme is designed to handle modules,
</span><span> 63</span><span> * you may configure the [[pathMap]] property like described above.
</span><span> 64</span><span> *
</span><span> 65</span><span> * @property string $basePath The root path of this theme. All resources of this theme are located under this
</span><span> 66</span><span> * directory.
</span><span> 67</span><span> * @property string $baseUrl The base URL (without ending slash) for this theme. All resources of this theme
</span><span> 68</span><span> * are considered to be under this base URL. This property is read-only.
</span><span> 69</span><span> *
</span><span> 70</span><span> * @author Qiang Xue <qiang.xue@gmail.com>
</span><span> 71</span><span> * @since 2.0
</span><span> 72</span><span>*/</span><span> 73</span><span>class</span><span> Theme extends Component
</span><span> 74</span><span>{
</span><span> 75</span><span>/*</span><span>*
</span><span> 76</span><span>     * @var array the mapping between view directories and their corresponding themed versions.
</span><span> 77</span><span>     * This property is used by [[applyTo()]] when a view is trying to apply the theme.
</span><span> 78</span><span>     * Path aliases can be used when specifying directories.
</span><span> 79</span><span>     * 路徑映射屬性 設(shè)置替換映射關(guān)系
</span><span> 80</span><span>     * If this property is empty or not set, a mapping [[Application::basePath]] to [[basePath]] will be used.
</span><span> 81</span><span>*/</span><span> 82</span><span>public</span><span> $pathMap;
</span><span> 83</span><span> 84</span><span>private</span> $_baseUrl;<span>//</span><span>設(shè)置要訪問(wèn)資源的url</span><span> 85</span><span> 86</span><span>/*</span><span>*
</span><span> 87</span><span>     * @return string the base URL (without ending slash) for this theme. All resources of this theme are considered
</span><span> 88</span><span>     * to be under this base URL. 返回當(dāng)前主題的基礎(chǔ)鏈接,其他資源都在鏈接里
</span><span> 89</span><span>*/</span><span> 90</span><span>public</span><span> function getBaseUrl()
</span><span> 91</span><span>    {
</span><span> 92</span><span>return</span> $<span>this</span>-><span>_baseUrl;
</span><span> 93</span><span>    }
</span><span> 94</span><span> 95</span><span>/*</span><span>*
</span><span> 96</span><span>     * @param $url string the base URL or path alias for this theme. All resources of this theme are considered
</span><span> 97</span><span>     * to be under this base URL. 設(shè)置基礎(chǔ)鏈接
</span><span> 98</span><span>*/</span><span> 99</span><span>public</span><span> function setBaseUrl($url)
</span><span>100</span><span>    {
</span><span>101</span>         $<span>this</span>->_baseUrl = rtrim(Yii::getAlias($url), <span>'</span><span>/</span><span>'</span><span>);
</span><span>102</span><span>    }
</span><span>103</span><span>104</span><span>private</span> $_basePath;<span>//</span><span>根路徑</span><span>105</span><span>106</span><span>/*</span><span>*
</span><span>107</span><span>     * @return string the root path of this theme. All resources of this theme are located under this directory.
</span><span>108</span><span>     * 得到當(dāng)前主題的根路徑
</span><span>109</span><span>     * @see pathMap
</span><span>110</span><span>*/</span><span>111</span><span>public</span><span> function getBasePath()
</span><span>112</span><span>    {
</span><span>113</span><span>return</span> $<span>this</span>-><span>_basePath;
</span><span>114</span><span>    }
</span><span>115</span><span>116</span><span>/*</span><span>*
</span><span>117</span><span>     * @param string $path the root path or path alias of this theme. All resources of this theme are located
</span><span>118</span><span>     * under this directory. 設(shè)置當(dāng)前主題根路徑
</span><span>119</span><span>     * @see pathMap
</span><span>120</span><span>*/</span><span>121</span><span>public</span><span> function setBasePath($path)
</span><span>122</span><span>    {
</span><span>123</span>         $<span>this</span>->_basePath =<span> Yii::getAlias($path);
</span><span>124</span><span>    }
</span><span>125</span><span>126</span><span>/*</span><span>*
</span><span>127</span><span>     * Converts a file to a themed file if possible. 將一個(gè)文件替換主題文件
</span><span>128</span><span>     * If there is no corresponding themed file, the original file will be returned.
</span><span>129</span><span>     * 沒(méi)有相應(yīng)的主題文件,返回原文件。
</span><span>130</span><span>     * @param string $path the file to be themed
</span><span>131</span><span>     * @return string the themed file, or the original file if the themed version is not available.
</span><span>132</span><span>     * @throws InvalidConfigException if [[basePath]] is not set
</span><span>133</span><span>*/</span><span>134</span><span>public</span><span> function applyTo($path)
</span><span>135</span><span>    {
</span><span>136</span>         $pathMap = $<span>this</span>->pathMap; <span>//</span><span>取得路徑映射</span><span>137</span><span>if</span> (empty($pathMap)) {<span>//</span><span>沒(méi)有設(shè)置值 拋出異常</span><span>138</span><span>if</span> (($basePath = $<span>this</span>->getBasePath()) === <span>null</span><span>) {
</span><span>139</span><span>throw</span><span>new</span> InvalidConfigException(<span>'</span><span>The "basePath" property must be set.</span><span>'</span><span>);
</span><span>140</span><span>            }
</span><span>141</span><span>//</span><span>設(shè)置值為[模塊根路徑=>主題根路徑]的形式</span><span>142</span>             $pathMap = [Yii::$app->getBasePath() =><span> [$basePath]];
</span><span>143</span><span>        }
</span><span>144</span><span>145</span>         $path = FileHelper::normalizePath($path);<span>//</span><span>對(duì)路徑中的"/"."\"進(jìn)行統(tǒng)一</span><span>146</span><span>147</span><span>foreach</span> ($pathMap <span>as</span> $<span>from</span> =><span> $tos) {
</span><span>148</span><span>//</span><span>映射數(shù)組中的來(lái)源</span><span>149</span>             $<span>from</span> = FileHelper::normalizePath(Yii::getAlias($<span>from</span><span>)) . DIRECTORY_SEPARATOR;
</span><span>150</span><span>if</span> (strpos($path, $<span>from</span>) === <span>0</span>) {<span>//</span><span>如果在$path中有可替換的舊值</span><span>151</span>                 $n = strlen($<span>from</span><span>);
</span><span>152</span><span>foreach</span> ((array) $tos <span>as</span><span> $to) {
</span><span>153</span>                     $to =<span> FileHelper::normalizePath(Yii::getAlias($to)) . DIRECTORY_SEPARATOR;
</span><span>154</span>                     $file = $to . substr($path, $n);<span>//</span><span>把$path中的$from替換為$to</span><span>155</span><span>if</span><span> (is_file($file)) {
</span><span>156</span><span>return</span> $file; <span>//</span><span>是文件直接返回</span><span>157</span><span>                    }
</span><span>158</span><span>                }
</span><span>159</span><span>            }
</span><span>160</span><span>        }
</span><span>161</span><span>162</span><span>return</span><span> $path;
</span><span>163</span><span>    }
</span><span>164</span><span>165</span><span>/*</span><span>*
</span><span>166</span><span>     * Converts a relative URL into an absolute URL using [[baseUrl]].
</span><span>167</span><span>     * 將一個(gè)相對(duì)URL轉(zhuǎn)換為絕對(duì)URL
</span><span>168</span><span>     * @param string $url the relative URL to be converted.要轉(zhuǎn)換的相對(duì)URL
</span><span>169</span><span>     * @return string the absolute URL  替換后的絕對(duì)URL
</span><span>170</span><span>     * @throws InvalidConfigException if [[baseUrl]] is not set
</span><span>171</span><span>*/</span><span>172</span><span>public</span><span> function getUrl($url)
</span><span>173</span><span>    {
</span><span>174</span><span>if</span> (($baseUrl = $<span>this</span>->getBaseUrl()) !== <span>null</span>) {<span>//</span><span>URL存在,進(jìn)行轉(zhuǎn)換</span><span>175</span><span>return</span> $baseUrl . <span>'</span><span>/</span><span>'</span> . ltrim($url, <span>'</span><span>/</span><span>'</span><span>);
</span><span>176</span>         } <span>else</span> {<span>//</span><span>不存在拋出異常</span><span>177</span><span>throw</span><span>new</span> InvalidConfigException(<span>'</span><span>The "baseUrl" property must be set.</span><span>'</span><span>);
</span><span>178</span><span>        }
</span><span>179</span><span>    }
</span><span>180</span><span>181</span><span>/*</span><span>*
</span><span>182</span><span>     * Converts a relative file path into an absolute one using [[basePath]].
</span><span>183</span><span>     * 通過(guò)相對(duì)路徑生成絕對(duì)路徑
</span><span>184</span><span>     * @param string $path the relative file path to be converted. 要轉(zhuǎn)換的相對(duì)文件路徑。
</span><span>185</span><span>     * @return string the absolute file path 轉(zhuǎn)換后的絕對(duì)路徑
</span><span>186</span><span>     * @throws InvalidConfigException if [[baseUrl]] is not set
</span><span>187</span><span>*/</span><span>188</span><span>public</span><span> function getPath($path)
</span><span>189</span><span>    {
</span><span>190</span><span>if</span> (($basePath = $<span>this</span>->getBasePath()) !== <span>null</span><span>) {
</span><span>191</span><span>//</span><span>返回拼接的路徑</span><span>192</span><span>return</span> $basePath . DIRECTORY_SEPARATOR . ltrim($path, <span>'</span><span>/\\</span><span>'</span><span>);
</span><span>193</span>         } <span>else</span><span> {
</span><span>194</span><span>throw</span><span>new</span> InvalidConfigException(<span>'</span><span>The "basePath" property must be set.</span><span>'</span><span>);
</span><span>195</span><span>        }
</span><span>196</span><span>    }
</span><span>197</span> }

以上就介紹了android源碼學(xué)習(xí) yii2源碼學(xué)習(xí)筆記十七),包括了android源碼學(xué)習(xí)方面的內(nèi)容,希望對(duì)PHP教程有興趣的朋友有所幫助。

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are some best practices for versioning a PHP-based API? What are some best practices for versioning a PHP-based API? Jun 14, 2025 am 12:27 AM

ToversionaPHP-basedAPIeffectively,useURL-basedversioningforclarityandeaseofrouting,separateversionedcodetoavoidconflicts,deprecateoldversionswithclearcommunication,andconsidercustomheadersonlywhennecessary.StartbyplacingtheversionintheURL(e.g.,/api/v

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

What are the differences between procedural and object-oriented programming paradigms in PHP? What are the differences between procedural and object-oriented programming paradigms in PHP? Jun 14, 2025 am 12:25 AM

Proceduralandobject-orientedprogramming(OOP)inPHPdiffersignificantlyinstructure,reusability,anddatahandling.1.Proceduralprogrammingusesfunctionsorganizedsequentially,suitableforsmallscripts.2.OOPorganizescodeintoclassesandobjects,modelingreal-worlden

What are weak references (WeakMap) in PHP, and when might they be useful? What are weak references (WeakMap) in PHP, and when might they be useful? Jun 14, 2025 am 12:25 AM

PHPdoesnothaveabuilt-inWeakMapbutoffersWeakReferenceforsimilarfunctionality.1.WeakReferenceallowsholdingreferenceswithoutpreventinggarbagecollection.2.Itisusefulforcaching,eventlisteners,andmetadatawithoutaffectingobjectlifecycles.3.YoucansimulateaWe

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

See all articles