


Yii2的深入学习--别名(Aliases),yii2aliases
在之前自动加载机制的文章中,我们有提到别名,提到 getAlias 方法,大家当时可能不太清楚,这到底是什么,今天我们就来说一下别名。
别名用来表示文件路径和 URL,这样就避免了将一些文件路径、URL以硬编码的方式写入代码中,或者多处出现一长串的文件路径、URL。
在 Yii2 中,一个别名必须以 @
字符开头,Yii2 预定义了大量可用的别名,预定义的别名如下:
- @yii 表示Yii框架所在的目录,也是 BaseYii.php 文件所在的位置
- @app 表示正在运行的应用的根目录
- @vendor 表示Composer 第三方库所在目录,一般是 @app/vendor 或 @app/../vendor
- @bower 表示 Bower 第三方库所在目录,一般是 @vendor/bower
- @npm 表示 NPM 第三方库所在目录,一般是 @vendor/npm
- @runtime 表示正在运行的应用的运行时用于存放运行时文件的目录,一般是 @app/runtime
- @webroot 表示正在运行的应用的入口文件 index.php 所在的目录,一般是 @app/web
- @web URL别名,表示当前应用的根URL,主要用于前端
- @common 表示通用文件夹
- @frontend 表示前台应用所在的文件夹
- @backend 表示后台应用所在的文件夹
- @console 表示命令行应用所在的文件夹
- 其他使用Composer安装的Yii扩展注册的二级别名
其中的 @common @frontend @backend 和 @console 在 baisc 的项目中是不会存在的。在 advanced 的项目中通常是定义在 common\config\bootstrap.php 文件中,其内容如下:
<?<span>php Yii</span>::setAlias('common', <span>dirname</span><span>(__DIR__)); Yii</span>::setAlias('frontend', <span>dirname</span>(<span>dirname</span>(__DIR__)) . '/frontend'<span>); Yii</span>::setAlias('backend', <span>dirname</span>(<span>dirname</span>(__DIR__)) . '/backend'<span>); Yii</span>::setAlias('console', <span>dirname</span>(<span>dirname</span>(__DIR__)) . '/console');
Yii2 中关于别名的设置和获取的方法都放在 BaseYii 类中,其结构基本如下:
<?<span>php </span><span>class</span><span> BaseYii { </span><span>/*</span><span>* * @var array registered path aliases * @see getAlias() * @see setAlias() * Yii 的路径别名的 Map, 默认 @yii 指向当前目录 </span><span>*/</span> <span>public</span> <span>static</span> <span>$aliases</span> = ['@yii' =><span> __DIR__]; </span><span>/*</span><span>* * Translates a path alias into an actual path. * 将别名转化为真实的路径 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> getAlias(<span>$alias</span>, <span>$throwException</span> = <span>true</span><span>) { </span>...<span> } </span><span>/*</span><span>* * Registers a path alias. * 用一个真实的路径注册一个别名 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> setAlias(<span>$alias</span>, <span>$path</span><span>) { </span>...<span> } }</span>
这是简化之后的 BaseYii 类的结构,其中有一个重要的变量 $aliases,两个重要的方法 getAlias 和 setAlias。$aliases 是存储 Yii2 路径别名的一个数组,key 是别名,value 是真实路径。getAlias 方法是根据别名获取到真实的地址,setAlias 是用一个真实的地址去注册一个别名。
先来看下 setAlias 方法,其内容如下:
<span>/*</span><span>* * Registers a path alias. * * 用一个真实的路径注册一个别名 * * A path alias is a short name representing a long path (a file path, a URL, etc.) * For example, we use '@yii' as the alias of the path to the Yii framework directory. * * A path alias must start with the character '@' so that it can be easily differentiated * from non-alias paths. * * Note that this method does not check if the given path exists or not. All it does is * to associate the alias with the path. * * Any trailing '/' and '\' characters in the given path will be trimmed. * * @param string $alias the alias name (e.g. "@yii"). It must start with a '@' character. * It may contain the forward slash '/' which serves as boundary character when performing * alias translation by [[getAlias()]]. * @param string $path the path corresponding to the alias. If this is null, the alias will * be removed. Trailing '/' and '\' characters will be trimmed. This can be * * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`) * - a URL (e.g. `http://www.yiiframework.com`) * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the * actual path first by calling [[getAlias()]]. * * @throws InvalidParamException if $path is an invalid alias. * @see getAlias() </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> setAlias(<span>$alias</span>, <span>$path</span><span>) { </span><span>if</span> (<span>strncmp</span>(<span>$alias</span>, '@', 1<span>)) { </span><span>//</span><span> 如果不是以 @ 开头,就将 @ 拼到开头</span> <span>$alias</span> = '@' . <span>$alias</span><span>; } </span><span>//</span><span> 获取 / 在 $alias 中首次出现的位置</span> <span>$pos</span> = <span>strpos</span>(<span>$alias</span>, '/'<span>); </span><span>//</span><span> 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容</span> <span>$root</span> = <span>$pos</span> === <span>false</span> ? <span>$alias</span> : <span>substr</span>(<span>$alias</span>, 0, <span>$pos</span><span>); </span><span>if</span> (<span>$path</span> !== <span>null</span><span>) { </span><span>//</span><span> 如果 $path 以 @ 开头,使用 getAlias 去获取路径,否则,就去除掉最右边的 /</span> <span>$path</span> = <span>strncmp</span>(<span>$path</span>, '@', 1) ? <span>rtrim</span>(<span>$path</span>, '\\/') : <span>static</span>::getAlias(<span>$path</span><span>); </span><span>if</span> (!<span>isset</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>//</span><span> 如果不存在这个 $root 的别名</span> <span>if</span> (<span>$pos</span> === <span>false</span><span>) { </span><span>//</span><span> 没有 /,就将 $path 直接赋值以为 $root 别名对应的路径</span> <span>static</span>::<span>$aliases</span>[<span>$root</span>] = <span>$path</span><span>; } </span><span>else</span><span> { </span><span>//</span><span> 否则,就将 $path 直接赋值为 $root 下的 $alias 的路径</span> <span>static</span>::<span>$aliases</span>[<span>$root</span>] = [<span>$alias</span> => <span>$path</span><span>]; } } </span><span>elseif</span> (<span>is_string</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>//</span><span> 如果存在,而且是个string类型</span> <span>if</span> (<span>$pos</span> === <span>false</span><span>) { </span><span>//</span><span> 没有 /,意味着 $alias 就是 $root,直接覆盖即可</span> <span>static</span>::<span>$aliases</span>[<span>$root</span>] = <span>$path</span><span>; } </span><span>else</span><span> { </span><span>//</span><span> 否则,就合并到一起</span> <span>static</span>::<span>$aliases</span>[<span>$root</span>] =<span> [ </span><span>$alias</span> => <span>$path</span>, <span>$root</span> => <span>static</span>::<span>$aliases</span>[<span>$root</span>],<span> ]; } } </span><span>else</span><span> { </span><span>//</span><span> 这种,正常是个 array 类型 // 直接添加进去即可</span> <span>static</span>::<span>$aliases</span>[<span>$root</span>][<span>$alias</span>] = <span>$path</span><span>; </span><span>//</span><span> krsort — 对数组按照键名逆向排序<br /> // 可以做到优先匹配长的别名</span> <span>krsort</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>]); } } </span><span>elseif</span> (<span>isset</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>//</span><span> $path 为空且对应的别名有值存在,就是要移除相应的别名</span> <span>if</span> (<span>is_array</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>//</span><span> 如果 $root 的别名对应一个 array,就只移除掉对应的别名即可</span> <span>unset</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span>][<span>$alias</span><span>]); } </span><span>elseif</span> (<span>$pos</span> === <span>false</span><span>) { </span><span>//</span><span> 如果 $root 的别名对应不是一个 array 而且 $root 就是 $alias,就移除这个 $root 的别名</span> <span>unset</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>]); } } }</span>
下面举几个例子来说明,别名写入后,$aliases 中的内容变化。
<span>//</span><span> 初始 BaseYii::aliases['@foo'] = 'path/to/foo'</span> Yii::setAlias('@foo', 'path/to/foo'<span>); </span><span>//</span><span> 直接覆盖 BaseYii::aliases['@foo'] = 'path/to/foo2'</span> Yii::setAlias('@foo', 'path/to/foo2'<span>); </span><span>/*</span><span>* * 新增 * BaseYii::aliases['@foo'] = [ * '@foo/bar' => 'path/to/foo/bar', * '@foo' => 'path/to/foo2', * ]; </span><span>*/</span><span> Yii</span>::setAlias('@foo/bar', 'path/to/foo/bar'<span>); </span><span>//</span><span> 初始 BaseYii::aliases['@bar'] = ['@bar/qux' => 'path/to/bar/qux'];</span> Yii::setAlias('@bar/qux', 'path/to/bar/qux'<span>); </span><span>//</span><span> 直接覆盖 BaseYii::aliases['@bar'] = ['@bar/qux' => 'path/to/bar/qux2'];</span> Yii::setAlias('@bar/qux', 'path/to/bar/qux2'<span>); </span><span>/*</span><span>* * 新增 * BaseYii::aliases['@bar'] = [ * '@bar/foo' => 'path/to/bar/foo', * '@bar/qux' => 'path/to/bar/qux2', * ]; </span><span>*/</span><span> Yii</span>::setAlias('@bar/foo', 'path/to/bar/foo'<span>); </span><span>/*</span><span>* * 新增 * BaseYii::aliases['@bar'] = [ * '@bar/foo' => 'path/to/bar/foo', * '@bar/qux' => 'path/to/bar/qux2', * '@bar' => 'path/to/bar', * ]; </span><span>*/</span><span> Yii</span>::setAlias('@bar', 'path/to/bar'<span>); </span><span>/*</span><span>* * 删除 * BaseYii::aliases['@bar'] = [ * '@bar/foo' => 'path/to/bar/foo', * '@bar' => 'path/to/bar', * ]; </span><span>*/</span><span> Yii</span>::setAlias('@bar/qux', <span>null</span><span>); </span><span>/*</span><span>* * 删除 * BaseYii::aliases['@bar'] = [ * '@bar/foo' => 'path/to/bar/foo', * ]; </span><span>*/</span><span> Yii</span>::setAlias('@bar', <span>null</span>);
再来看一下 getAlias 方法,其内容如下:
<span>/*</span><span>* * Translates a path alias into an actual path. * 将别名转化为真实的路径 * * The translation is done according to the following procedure: * * 1. If the given alias does not start with '@', it is returned back without change; * 2. Otherwise, look for the longest registered alias that matches the beginning part * of the given alias. If it exists, replace the matching part of the given alias with * the corresponding registered path. * 3. Throw an exception or return false, depending on the `$throwException` parameter. * * For example, by default '@yii' is registered as the alias to the Yii framework directory, * say '/path/to/yii'. The alias '@yii/web' would then be translated into '/path/to/yii/web'. * * If you have registered two aliases '@foo' and '@foo/bar'. Then translating '@foo/bar/config' * would replace the part '@foo/bar' (instead of '@foo') with the corresponding registered path. * This is because the longest alias takes precedence. * * However, if the alias to be translated is '@foo/barbar/config', then '@foo' will be replaced * instead of '@foo/bar', because '/' serves as the boundary character. * * Note, this method does not check if the returned path exists or not. * * @param string $alias the alias to be translated. * @param boolean $throwException whether to throw an exception if the given alias is invalid. * If this is false and an invalid alias is given, false will be returned by this method. * @return string|boolean the path corresponding to the alias, false if the root alias is not previously registered. * @throws InvalidParamException if the alias is invalid while $throwException is true. * @see setAlias() </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> getAlias(<span>$alias</span>, <span>$throwException</span> = <span>true</span><span>) { </span><span>/*</span><span>* * strncmp — 二进制安全比较字符串开头的若干个字符 * int strncmp ( string $str1 , string $str2 , int $len ) * 如果 $alias 不是以 '@' 开头的,就不是一个 Yii 的别名 </span><span>*/</span> <span>if</span> (<span>strncmp</span>(<span>$alias</span>, '@', 1<span>)) { </span><span>//</span><span> not an alias</span> <span>return</span> <span>$alias</span><span>; } </span><span>//</span><span> 获取 / 在 $alias 中首次出现的位置</span> <span>$pos</span> = <span>strpos</span>(<span>$alias</span>, '/'<span>); </span><span>//</span><span> 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容</span> <span>$root</span> = <span>$pos</span> === <span>false</span> ? <span>$alias</span> : <span>substr</span>(<span>$alias</span>, 0, <span>$pos</span><span>); </span><span>//</span><span> 如果存在 $root 的别名</span> <span>if</span> (<span>isset</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>if</span> (<span>is_string</span>(<span>static</span>::<span>$aliases</span>[<span>$root</span><span>])) { </span><span>//</span><span> 如果 $root 对应的别名是一个字符串,之直接返回 $aliases[$root] 或者 $aliases[$root] . substr($alias, $pos) // 当 $root 就是 $alias 返回 $aliases[$root], 否则就在拼接上 $alias 除去 $root 后,剩下的字符串</span> <span>return</span> <span>$pos</span> === <span>false</span> ? <span>static</span>::<span>$aliases</span>[<span>$root</span>] : <span>static</span>::<span>$aliases</span>[<span>$root</span>] . <span>substr</span>(<span>$alias</span>, <span>$pos</span><span>); } </span><span>else</span><span> { </span><span>//</span><span> 否则,要遍历整个 $aliases[$root] 数组,找到 $name 与 $alias 相同的值,返回 $path . substr($alias, strlen($name)) // 其实是返回了 $path 拼接上 $alias 除去 $root 后,剩下的字符串</span> <span>foreach</span> (<span>static</span>::<span>$aliases</span>[<span>$root</span>] <span>as</span> <span>$name</span> => <span>$path</span><span>) { </span><span>if</span> (<span>strpos</span>(<span>$alias</span> . '/', <span>$name</span> . '/') === 0<span>) { </span><span>return</span> <span>$path</span> . <span>substr</span>(<span>$alias</span>, <span>strlen</span>(<span>$name</span><span>)); } } } } </span><span>if</span> (<span>$throwException</span><span>) { </span><span>throw</span> <span>new</span> InvalidParamException("Invalid path alias: <span>$alias</span>"<span>); } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } }</span>
好了,关于别名就先说这么多~~
对 Yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 Yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 Yii2 源码的注释。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)