mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 14:46:19 +08:00 
			
		
		
		
	translate concept-aliases and autoloading [skip cii]
This commit is contained in:
		
							
								
								
									
										119
									
								
								docs/guide-zh-CN/concept-aliases.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								docs/guide-zh-CN/concept-aliases.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,119 @@
 | 
			
		||||
路径别名(Aliases)
 | 
			
		||||
=======
 | 
			
		||||
 | 
			
		||||
路径别名(Path Alias 简称别名)用作代表文件路径和 URL,主要为了避免在代码中硬编码一些绝对路径和 
 | 
			
		||||
URL。一个别名必须以 `@` 字符开头,以区别于传统的文件/目录路径或 URL。举栗,别名 `@yii` 
 | 
			
		||||
指的是 Yii 框架本身的安装目录,而 `@web` 表示的是当前运行应用的根 URL(base URL)。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
定义别名 <a name="defining-aliases"></a>
 | 
			
		||||
----------------
 | 
			
		||||
 | 
			
		||||
你可以调用 [[Yii::setAlias()]] 来给指定路径/URL 定义别名。栗子:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
// 文件路径的别名
 | 
			
		||||
Yii::setAlias('@foo', '/path/to/foo');
 | 
			
		||||
 | 
			
		||||
// URL 的别名
 | 
			
		||||
Yii::setAlias('@bar', 'http://www.example.com');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
> 注意:别名所指向的文件路径或 URL 不一定是真实存在的文件或资源哦。
 | 
			
		||||
 | 
			
		||||
用一个别名,你能通过在后面接续斜杠 `/` 以及若干路径片段得到一个新的别名(无需调用 
 | 
			
		||||
[[Yii::setAlias()]])。我们把通过 [[Yii::setAlias()]] 定义的别名成为根别名 
 | 
			
		||||
*root aliases*,而用他们衍生出去的别名成为衍生别名 *derived aliases*。比如,`@foo` 就是跟别名,而 `@foo/bar/file.php` 
 | 
			
		||||
是一个衍生别名。
 | 
			
		||||
 | 
			
		||||
你还可以用别名定义新别名(根别名与衍生别名均可):
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
Yii::setAlias('@foobar', '@foo/bar');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
根别名通常在 [引导(bootstrapping)](runtime-bootstrapping.md) 阶段定义。比如你可以在 
 | 
			
		||||
[入口脚本](structure-entry-scripts.md) 里调用 [[Yii::setAlias()]]。为了方便起见呢,[应用主体(Application)](structure-applications.md) 
 | 
			
		||||
提供了一个名为 `aliases` 的可写属性,你可以在应用[配置文件](concept-configurations.md)中设置它,就像这样:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
return [
 | 
			
		||||
    // ...
 | 
			
		||||
    'aliases' => [
 | 
			
		||||
        '@foo' => '/path/to/foo',
 | 
			
		||||
        '@bar' => 'http://www.example.com',
 | 
			
		||||
    ],
 | 
			
		||||
];
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
解析别名 <a name="resolving-aliases"></a>
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
你可以调用 [[Yii::getAlias()]] 命令来解析一个根别名到他所对应的文件路径或 URL。同样的页面也可以用于解析衍生别名。比如:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
echo Yii::getAlias('@foo');               // 显示:/path/to/foo
 | 
			
		||||
echo Yii::getAlias('@bar');               // 显示:http://www.example.com
 | 
			
		||||
echo Yii::getAlias('@foo/bar/file.php');  // 显示:/path/to/foo/bar/file.php
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
由衍生别名所代指的路径/URL 是通过替换掉衍生别名中的根别名部分得到的。
 | 
			
		||||
 | 
			
		||||
> 注意:[[Yii::getAlias()]] 不检查结果路径/URL 所指向的资源是否真实存在。
 | 
			
		||||
 | 
			
		||||
根别名可能也会包含斜杠 `/` 字符。[[Yii::getAlias()]] 足够聪明,能知道一个别名中的哪个部分是根别名,因此能正确解析文件路径/URL。比如:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
Yii::setAlias('@foo', '/path/to/foo');
 | 
			
		||||
Yii::setAlias('@foo/bar', '/path2/bar');
 | 
			
		||||
Yii::getAlias('@foo/test/file.php');  // 显示:/path/to/foo/test/file.php
 | 
			
		||||
Yii::getAlias('@foo/bar/file.php');   // 显示:/path2/bar/file.php
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
若 `@foo/bar` 未被定义为根别名,最后一行语句会显示为 `/path/to/foo/bar/file.php`。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
使用别名 <a name="using-aliases"></a>
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
别名在 Yii 的很多地方都会被正确识别,而无需调用 [[Yii::getAlias()]] 
 | 
			
		||||
来把它们转换为路径/URL。比如,[[yii\caching\FileCache::cachePath]] 能同时接受文件路径或是代表文件路径的别名,多亏了 `@` 前缀,它区分开了文件路径与别名。
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
use yii\caching\FileCache;
 | 
			
		||||
 | 
			
		||||
$cache = new FileCache([
 | 
			
		||||
    'cachePath' => '@runtime/cache',
 | 
			
		||||
]);
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
请关注下 API 文档了解属性或方法参数是否支持别名。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
预定义的别名 <a name="predefined-aliases"></a>
 | 
			
		||||
------------------
 | 
			
		||||
 | 
			
		||||
Yii 预定义了一系列别名来简化频繁引用常用路径和 URL的需求。
 | 
			
		||||
在核心框架中已经预定义有以下别名:
 | 
			
		||||
 | 
			
		||||
- `@yii` - `BaseYii.php` 文件所在的目录(也被称为框架安装目录)
 | 
			
		||||
- `@app` - 当前运行的应用 [[yii\base\Application::basePath|根路径(base path)]] 
 | 
			
		||||
- `@runtime` - 当前运行的应用的 [[yii\base\Application::runtimePath|运行环境(runtime)路径]] 
 | 
			
		||||
- `@vendor` - [[yii\base\Application::vendorPath|Composer 供应商目录]]
 | 
			
		||||
- `@webroot` - 当前运行应用的 Web 入口目录
 | 
			
		||||
- `@web` - 当前运行应用的根 URL
 | 
			
		||||
 | 
			
		||||
`@yii` 别名是在[入口脚本](structure-entry-scripts.md)里包含 `Yii.php` 文件时定义的,其他的别名都是在[配置应用](concept-configurations.md)的时候,于应用的构造器内定义的。
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
扩展的别名 <a name="extension-aliases"></a>
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
每一个通过 Composer 安装的 [扩展](structure-extensions.md) 都自动添加了一个别名。该别名会以该扩展在 `composer.json` 
 | 
			
		||||
文件中所声明的根命名空间为名,且他直接代指该包的根目录。比如,如果你安装有 `yiisoft/yii2-jui` 扩展,你会自动得到 
 | 
			
		||||
`@yii/jui` 别名,它定义于[引导启动](runtime-bootstrapping.md)阶段:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
Yii::setAlias('@yii/jui', 'VendorPath/yiisoft/yii2-jui');
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										92
									
								
								docs/guide-zh-CN/concept-autoloading.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								docs/guide-zh-CN/concept-autoloading.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,92 @@
 | 
			
		||||
类自动加载(Autoloading)
 | 
			
		||||
=================
 | 
			
		||||
 | 
			
		||||
Yii relies on the [class autoloading mechanism](http://www.php.net/manual/en/language.oop5.autoload.php)
 | 
			
		||||
to locate and include required class files. It provides a high-performance class autoloader that is compliant to the
 | 
			
		||||
[PSR-4 标准](https://github.com/php-fig/fig-standards/blob/master/proposed/psr-4-autoloader/psr-4-autoloader.md).
 | 
			
		||||
The autoloader is installed when you include the `Yii.php` file.
 | 
			
		||||
 | 
			
		||||
> Note: For simplicity of description, in this section we will only talk about autoloading of classes. However, keep in
 | 
			
		||||
  mind that the content we are describing here applies to autoloading of interfaces and traits as well.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
使用 Yii 自动加载器 <a name="using-yii-autoloader"></a>
 | 
			
		||||
------------------------
 | 
			
		||||
 | 
			
		||||
To make use of the Yii class autoloader, you should follow two simple rules when creating and naming your classes:
 | 
			
		||||
 | 
			
		||||
* Each class must be under some namespace (e.g. `foo\bar\MyClass`).
 | 
			
		||||
* Each class must be saved in an individual file whose path is determined by the following algorithm:
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
// $className is a fully qualified class name with the leading backslash
 | 
			
		||||
$classFile = Yii::getAlias('@' . str_replace('\\', '/', $className) . '.php');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
For example, if a class name is `foo\bar\MyClass`, the [alias](concept-aliases.md) for the corresponding class file path
 | 
			
		||||
would be `@foo/bar/MyClass.php`. In order for this alias to be able to be resolved into a file path,
 | 
			
		||||
either `@foo` or `@foo/bar` must be a [root alias](concept-aliases.md#defining-aliases).
 | 
			
		||||
 | 
			
		||||
When you are using the [Basic Application Template](start-basic.md), you may put your classes under the top-level
 | 
			
		||||
namespace `app` so that they can be autoloaded by Yii without the need of defining a new alias. This is because
 | 
			
		||||
`@app` is a [predefined alias](concept-aliases.md#predefined-aliases), and a class name like `app\components\MyClass`
 | 
			
		||||
can be resolved into the class file `AppBasePath/components/MyClass.php`, according to the algorithm we just described.
 | 
			
		||||
 | 
			
		||||
In the [Advanced Application Template](tutorial-advanced-app.md), each tier has its own root alias. For example,
 | 
			
		||||
the front-end tier has a root alias `@frontend` while the back-end tier `@backend`. As a result, you may
 | 
			
		||||
put the front-end classes under the namespace `frontend` while the back-end classes under `backend`. This will
 | 
			
		||||
allow these classes to be autoloaded by the Yii autoloader.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
类映射表(Class Map) <a name="class-map"></a>
 | 
			
		||||
---------
 | 
			
		||||
 | 
			
		||||
The Yii class autoloader supports the *class map* feature which maps class names to the corresponding class file paths.
 | 
			
		||||
When the autoloader is loading a class, it will first check if the class is found in the map. If so, the corresponding
 | 
			
		||||
file path will be included directly without further check. This makes class autoloading super fast. In fact,
 | 
			
		||||
all core Yii classes are being autoloaded this way.
 | 
			
		||||
 | 
			
		||||
You may add a class to the class map `Yii::$classMap` as follows,
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
Yii::$classMap['foo\bar\MyClass'] = 'path/to/MyClass.php';
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
[Aliases](concept-aliases.md) can be used to specify class file paths. You should set the class map in the
 | 
			
		||||
[bootstrapping](runtime-bootstrapping.md) process so that the map is ready before your classes are used.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
用其他自动加载器 <a name="using-other-autoloaders"></a>
 | 
			
		||||
-----------------------
 | 
			
		||||
 | 
			
		||||
Because Yii embraces Composer as a package dependency manager, it is recommended that you also install
 | 
			
		||||
the Composer autoloader. If you are using some 3rd-party libraries that have their autoloaders, you should
 | 
			
		||||
also install them.
 | 
			
		||||
 | 
			
		||||
When you are using the Yii autoloader together with other autoloaders, you should include the `Yii.php` file
 | 
			
		||||
*after* all other autoloaders are installed. This will make the Yii autoloader to be the first one responding to
 | 
			
		||||
any class autoloading request. For example, the following code is extracted from
 | 
			
		||||
the [entry script](structure-entry-scripts.md) of the [Basic Application Template](start-basic.md). The first
 | 
			
		||||
line installs the Composer autoloader, while the second line installs the Yii autoloader.
 | 
			
		||||
 | 
			
		||||
```php
 | 
			
		||||
require(__DIR__ . '/../vendor/autoload.php');
 | 
			
		||||
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
You may use the Composer autoloader alone without the Yii autoloader. However, by doing so, the performance
 | 
			
		||||
of your class autoloading may be degraded, and you must follow the rules set by Composer in order for your classes
 | 
			
		||||
to be autoloadable.
 | 
			
		||||
 | 
			
		||||
> Info: If you do not want to use the Yii autoloader, you must create your own version of the `Yii.php` file
 | 
			
		||||
  and include it in your [entry script](structure-entry-scripts.md).
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
自动加载扩展类 <a name="autoloading-extension-classes"></a>
 | 
			
		||||
-----------------------------
 | 
			
		||||
 | 
			
		||||
The Yii autoloader is capable of autoloading [extension](structure-extensions.md) classes. The sole requirement
 | 
			
		||||
is that an extension specifies the `autoload` section correctly in its `composer.json` file. Please refer to the
 | 
			
		||||
[Composer documentation](https://getcomposer.org/doc/04-schema.md#autoload) for more details about specifying `autoload`.
 | 
			
		||||
 | 
			
		||||
In case you do not use the Yii autoloader, the Composer autoloader can still autoload extension classes for you.
 | 
			
		||||
		Reference in New Issue
	
	Block a user