Yii2的深入學習--別名(Aliases),yii2aliases_PHP教程

來源:互聯網
上載者:User

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 檔案中,其內容如下:

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 = ['@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 源碼的注釋。

http://www.bkjia.com/PHPjc/1063510.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1063510.htmlTechArticleYii2的深入學習--別名(Aliases),yii2aliases 在之前自動載入機制的文章中,我們有提到別名,提到getAlias 方法,大家當時可能不太清楚,這到...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.