您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Yii2的深入学习-别号(Aliases)

2026/1/9 5:08:23发布11次查看
yii2的深入学习--别名(aliases)
在之前自动加载机制的文章中,我们有提到别名,提到 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 文件中,其内容如下:
phpyii::setalias('common', dirname(__dir__));yii::setalias('frontend', dirname(dirname(__dir__)) . '/frontend');yii::setalias('backend', dirname(dirname(__dir__)) . '/backend');yii::setalias('console', dirname(dirname(__dir__)) . '/console');
yii2 中关于别名的设置和获取的方法都放在 baseyii 类中,其结构基本如下:
phpclass baseyii{ /** * @var array registered path aliases * @see getalias() * @see setalias() * yii 的路径别名的 map, 默认 @yii 指向当前目录 */ public static $aliases = [[email protected]' => __dir__]; /** * translates a path alias into an actual path. * 将别名转化为真实的路径 */ public static function getalias($alias, $throwexception = true) { ... } /** * registers a path alias. * 用一个真实的路径注册一个别名 */ public static function setalias($alias, $path) { ... }}
这是简化之后的 baseyii 类的结构,其中有一个重要的变量 $aliases,两个重要的方法 getalias 和 setalias。$aliases 是存储 yii2 路径别名的一个数组,key 是别名,value 是真实路径。getalias 方法是根据别名获取到真实的地址,setalias 是用一个真实的地址去注册一个别名。
先来看下 setalias 方法,其内容如下:
/** * 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 [email protected]' 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. [email protected]/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() */ public static function setalias($alias, $path) { if (strncmp($alias, '@', 1)) { // 如果不是以 @ 开头,就将 @ 拼到开头 $alias = '@' . $alias; } // 获取 / 在 $alias 中首次出现的位置 $pos = strpos($alias, '/'); // 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容 $root = $pos === false ? $alias : substr($alias, 0, $pos); if ($path !== null) { // 如果 $path 以 @ 开头,使用 getalias 去获取路径,否则,就去除掉最右边的 / $path = strncmp($path, '@', 1) ? rtrim($path, '\\/') : static::getalias($path); if (!isset(static::$aliases[$root])) { // 如果不存在这个 $root 的别名 if ($pos === false) { // 没有 /,就将 $path 直接赋值以为 $root 别名对应的路径 static::$aliases[$root] = $path; } else { // 否则,就将 $path 直接赋值为 $root 下的 $alias 的路径 static::$aliases[$root] = [$alias => $path]; } } elseif (is_string(static::$aliases[$root])) { // 如果存在,而且是个string类型 if ($pos === false) { // 没有 /,意味着 $alias 就是 $root,直接覆盖即可 static::$aliases[$root] = $path; } else { // 否则,就合并到一起 static::$aliases[$root] = [ $alias => $path, $root => static::$aliases[$root], ]; } } else { // 这种,正常是个 array 类型 // 直接添加进去即可 static::$aliases[$root][$alias] = $path; // krsort — 对数组按照键名逆向排序
  // 可以做到优先匹配长的别名 krsort(static::$aliases[$root]); } } elseif (isset(static::$aliases[$root])) { // $path 为空且对应的别名有值存在,就是要移除相应的别名 if (is_array(static::$aliases[$root])) { // 如果 $root 的别名对应一个 array,就只移除掉对应的别名即可 unset(static::$aliases[$root][$alias]); } elseif ($pos === false) { // 如果 $root 的别名对应不是一个 array 而且 $root 就是 $alias,就移除这个 $root 的别名 unset(static::$aliases[$root]); } } }
下面举几个例子来说明,别名写入后,$aliases 中的内容变化。
// 初始 baseyii::aliases[[email protected]'] = 'path/to/foo'yii::setalias([email protected]', 'path/to/foo');// 直接覆盖 baseyii::aliases[[email protected]'] = 'path/to/foo2'yii::setalias([email protected]', 'path/to/foo2');/*** 新增* baseyii::aliases[[email protected]'] = [* [email protected]/bar' => 'path/to/foo/bar',* [email protected]' => 'path/to/foo2',* ];*/yii::setalias([email protected]/bar', 'path/to/foo/bar');// 初始 baseyii::aliases[[email protected]'] = [[email protected]/qux' => 'path/to/bar/qux'];yii::setalias([email protected]/qux', 'path/to/bar/qux');// 直接覆盖 baseyii::aliases[[email protected]'] = [[email protected]/qux' => 'path/to/bar/qux2'];yii::setalias([email protected]/qux', 'path/to/bar/qux2');/*** 新增* baseyii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]/qux' => 'path/to/bar/qux2',* ];*/yii::setalias([email protected]/foo', 'path/to/bar/foo');/*** 新增* baseyii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]/qux' => 'path/to/bar/qux2',* [email protected]' => 'path/to/bar',* ];*/yii::setalias([email protected]', 'path/to/bar');/*** 删除* baseyii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* [email protected]' => 'path/to/bar',* ];*/yii::setalias([email protected]/qux', null);/*** 删除* baseyii::aliases[[email protected]'] = [* [email protected]/foo' => 'path/to/bar/foo',* ];*/yii::setalias([email protected]', null);
再来看一下 getalias 方法,其内容如下:
/** * 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 [email protected]' is registered as the alias to the yii framework directory, * say '/path/to/yii'. the alias [email protected]/web' would then be translated into '/path/to/yii/web'. * * if you have registered two aliases [email protected]' and [email protected]/bar'. then translating [email protected]/bar/config' * would replace the part [email protected]/bar' (instead of [email protected]') with the corresponding registered path. * this is because the longest alias takes precedence. * * however, if the alias to be translated is [email protected]/barbar/config', then [email protected]' will be replaced * instead of [email protected]/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() */ public static function getalias($alias, $throwexception = true) { /** * strncmp — 二进制安全比较字符串开头的若干个字符 * int strncmp ( string $str1 , string $str2 , int $len ) * 如果 $alias 不是以 '@' 开头的,就不是一个 yii 的别名 */ if (strncmp($alias, '@', 1)) { // not an alias return $alias; } // 获取 / 在 $alias 中首次出现的位置 $pos = strpos($alias, '/'); // 如果 / 不存在,$root 就是整个 $alias,否则就是 $alias 中 / 前的内容 $root = $pos === false ? $alias : substr($alias, 0, $pos); // 如果存在 $root 的别名 if (isset(static::$aliases[$root])) { if (is_string(static::$aliases[$root])) { // 如果 $root 对应的别名是一个字符串,之直接返回 $aliases[$root] 或者 $aliases[$root] . substr($alias, $pos) // 当 $root 就是 $alias 返回 $aliases[$root], 否则就在拼接上 $alias 除去 $root 后,剩下的字符串 return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos); } else { // 否则,要遍历整个 $aliases[$root] 数组,找到 $name 与 $alias 相同的值,返回 $path . substr($alias, strlen($name)) // 其实是返回了 $path 拼接上 $alias 除去 $root 后,剩下的字符串 foreach (static::$aliases[$root] as $name => $path) { if (strpos($alias . '/', $name . '/') === 0) { return $path . substr($alias, strlen($name)); } } } } if ($throwexception) { throw new invalidparamexception(invalid path alias: $alias); } else { return false; } }
好了,关于别名就先说这么多~~
对 yii2 源码有兴趣的同学可以关注项目 yii2-2.0.3-annotated,现在在上面已经添加了不少关于 yii2 源码的注释,之后还会继续添加~
有兴趣的同学也可以参与进来,提交 yii2 源码的注释。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product