国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Yii2的深入學習--別名(Aliases)

2019-11-14 14:43:05
字體:
供稿:網(wǎng)友

在之前自動加載機制的文章中,我們有提到別名,提到 getAlias 方法,大家當時可能不太清楚,這到底是什么,今天我們就來說一下別名。

別名用來表示文件路徑和 URL,這樣就避免了將一些文件路徑、URL以硬編碼的方式寫入代碼中,或者多處出現(xiàn)一長串的文件路徑、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 文件中,其內(nèi)容如下:

<?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 中關(guān)于別名的設置和獲取的方法都放在 BaseYii 類中,其結(jié)構(gòu)基本如下:

<?phpclass 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.     * 將別名轉(zhuǎn)化為真實的路徑     */    public static function getAlias($alias, $throwException = true)    {        ...    }    /**     * Registers a path alias.     * 用一個真實的路徑注冊一個別名     */    public static function setAlias($alias, $path)    {        ...    }}

這是簡化之后的 BaseYii 類的結(jié)構(gòu),其中有一個重要的變量 $aliases,兩個重要的方法 getAlias 和 setAlias。$aliases 是存儲 Yii2 路徑別名的一個數(shù)組,key 是別名,value 是真實路徑。getAlias 方法是根據(jù)別名獲取到真實的地址,setAlias 是用一個真實的地址去注冊一個別名。

先來看下 setAlias 方法,其內(nèi)容如下:

    /**     * 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 中首次出現(xiàn)的位置        $pos = strpos($alias, '/');        // 如果 / 不存在,$root 就是整個 $alias,否則就是 $alias 中 / 前的內(nèi)容        $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 — 對數(shù)組按照鍵名逆向排序
          // 可以做到優(yōu)先匹配長的別名
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 中的內(nèi)容變化。

// 初始 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 方法,其內(nèi)容如下:

    /**     * Translates a path alias into an actual path.     * 將別名轉(zhuǎn)化為真實的路徑     *     * 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 中首次出現(xiàn)的位置        $pos = strpos($alias, '/');        // 如果 / 不存在,$root 就是整個 $alias,否則就是 $alias 中 / 前的內(nèi)容        $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] 數(shù)組,找到 $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;        }    }

好了,關(guān)于別名就先說這么多~~

 

對 Yii2 源碼有興趣的同學可以關(guān)注項目 yii2-2.0.3-annotated,現(xiàn)在在上面已經(jīng)添加了不少關(guān)于 Yii2 源碼的注釋,之后還會繼續(xù)添加~

有興趣的同學也可以參與進來,提交 Yii2 源碼的注釋。

 


上一篇:CI整合Smarty

下一篇:驗證碼類

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广灵县| 易门县| 原平市| 车致| 大关县| 丹棱县| 梅河口市| 谢通门县| 兴山县| 阿拉善左旗| 彭山县| 广汉市| 永善县| 许昌市| 新田县| 玛纳斯县| 甘泉县| 武乡县| 安福县| 恭城| 鹿邑县| 商南县| 时尚| 莱芜市| 浦北县| 镇安县| 卢龙县| 克拉玛依市| 曲水县| 渭南市| 南平市| 新河县| 沐川县| 河西区| 忻城县| 墨竹工卡县| 昆山市| 原阳县| 封开县| 水城县| 紫金县|