热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Yii2的深入学习--别名(Aliases),yii2aliases_PHP教程

Yii2的深入学习--别名(Aliases),yii2aliases。Yii2的深入学习--别名(Aliases),yii2aliases在之前自动加载机制的文章中,我们有提到别名,提到getAlias方法,大家当时可能不太清楚,这到

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 文件中,其内容如下:

php
Yii::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 类中,其结构基本如下:

php
class BaseYii
{
    /**
     * @var array registered path aliases
     * @see getAlias()
     * @see setAlias()
     * Yii 的路径别名的 Map, 默认 @yii 指向当前目录
     */
    public static $aliases = ['@yii' => __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 '@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()
     */
    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['@foo'] = 'path/to/foo'
Yii::setAlias('@foo', 'path/to/foo');

// 直接覆盖 BaseYii::aliases['@foo'] = 'path/to/foo2'
Yii::setAlias('@foo', 'path/to/foo2');

/**
* 新增
* BaseYii::aliases['@foo'] = [
*     '@foo/bar' => 'path/to/foo/bar',
*     '@foo' => 'path/to/foo2',
* ];
*/
Yii::setAlias('@foo/bar', 'path/to/foo/bar');

// 初始 BaseYii::aliases['@bar'] = ['@bar/qux' => 'path/to/bar/qux'];
Yii::setAlias('@bar/qux', 'path/to/bar/qux');

// 直接覆盖 BaseYii::aliases['@bar'] = ['@bar/qux' => 'path/to/bar/qux2'];
Yii::setAlias('@bar/qux', 'path/to/bar/qux2');

/**
* 新增
* BaseYii::aliases['@bar'] = [
*     '@bar/foo' => 'path/to/bar/foo',
*     '@bar/qux' => 'path/to/bar/qux2',
* ];
*/
Yii::setAlias('@bar/foo', 'path/to/bar/foo');

/**
* 新增
* BaseYii::aliases['@bar'] = [
*     '@bar/foo' => 'path/to/bar/foo',
*     '@bar/qux' => 'path/to/bar/qux2',
*     '@bar' => 'path/to/bar',
* ];
*/
Yii::setAlias('@bar', 'path/to/bar');

/**
* 删除
* BaseYii::aliases['@bar'] = [
*     '@bar/foo' => 'path/to/bar/foo',
*     '@bar' => 'path/to/bar',
* ];
*/
Yii::setAlias('@bar/qux', null);

/**
* 删除
* BaseYii::aliases['@bar'] = [
*     '@bar/foo' => 'path/to/bar/foo',
* ];
*/
Yii::setAlias('@bar', 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 '@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()
     */
    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 源码的注释。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063510.htmlTechArticleYii2的深入学习--别名(Aliases),yii2aliases 在之前自动加载机制的文章中,我们有提到别名,提到getAlias 方法,大家当时可能不太清楚,这到...


推荐阅读
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • PDF内容编辑的两种小方法,你知道怎么操作吗?
    本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ... [详细]
  • 本文介绍了前端人员必须知道的三个问题,即前端都做哪些事、前端都需要哪些技术,以及前端的发展阶段。初级阶段包括HTML、CSS、JavaScript和jQuery的基础知识。进阶阶段涵盖了面向对象编程、响应式设计、Ajax、HTML5等新兴技术。高级阶段包括架构基础、模块化开发、预编译和前沿规范等内容。此外,还介绍了一些后端服务,如Node.js。 ... [详细]
  • 必须先赞下国人npm库作品:node-images(https:github.comzhangyuanweinode-images),封装了跨平台的C++逻辑,形成nodejsAP ... [详细]
  • 1.移除consol.log()的babel插件安装:npmibabel-plugin-transform-remove-console-D配置:babel.config.js:这 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了响应式页面的概念和实现方式,包括针对不同终端制作特定页面和制作一个页面适应不同终端的显示。分析了两种实现方式的优缺点,提出了选择方案的建议。同时,对于响应式页面的需求和背景进行了讨论,解释了为什么需要响应式页面。 ... [详细]
  • HTML5网页模板怎么加百度统计?
    本文介绍了如何在HTML5网页模板中加入百度统计,并对模板文件、css样式表、js插件库等内容进行了说明。同时还解答了关于HTML5网页模板的使用方法、表单提交、域名和空间的问题,并介绍了如何使用Visual Studio 2010创建HTML5模板。此外,还提到了使用Jquery编写美好的HTML5前端框架模板的方法,以及制作企业HTML5网站模板和支持HTML5的CMS。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • Netty源代码分析服务器端启动ServerBootstrap初始化
    本文主要分析了Netty源代码中服务器端启动的过程,包括ServerBootstrap的初始化和相关参数的设置。通过分析NioEventLoopGroup、NioServerSocketChannel、ChannelOption.SO_BACKLOG等关键组件和选项的作用,深入理解Netty服务器端的启动过程。同时,还介绍了LoggingHandler的作用和使用方法,帮助读者更好地理解Netty源代码。 ... [详细]
  • React 小白初入门
    推荐学习:React官方文档:https:react.docschina.orgReact菜鸟教程:https:www.runoob.c ... [详细]
  • vuecli创建项目(详情步骤)
    1、安装node环境2、下载vue和vue-cli脚手架命令行输入npm ... [详细]
  • RN即ReactNative基于React框架针对移动端的跨平台框架,在学习RN前建议最好熟悉下html,css,js,当然如果比较急,那就直接上手吧,毕竟用学习前面基础的时间,R ... [详细]
  • 前言:原本纠结于Web模板,选了Handlebars。后来发现页面都是弱逻辑的,不支持复杂逻辑表达式。几乎要放弃之际,想起了Javascript中ev ... [详细]
author-avatar
tanglei52017
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有