Browse Source

update character

tags/2.0.16
unknown 10 years ago
parent
commit
8a77413a21
  1. 4
      docs/guide-zh-CN/db-elasticsearch.md
  2. 128
      docs/guide-zh-CN/db-migrations.md
  3. 4
      docs/guide-zh-CN/db-mongodb.md
  4. 4
      docs/guide-zh-CN/db-redis.md
  5. 4
      docs/guide-zh-CN/db-sphinx.md
  6. 30
      docs/guide-zh-CN/helper-overview.md
  7. 40
      docs/guide-zh-CN/helper-url.md
  8. 200
      docs/guide-zh-CN/structure-extensions.md

4
docs/guide-zh-CN/db-elasticsearch.md

@ -1,6 +1,6 @@
Elasticsearch
=============
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
>
> 现在还没有内容。
> 现在还没有内容。

128
docs/guide-zh-CN/db-migrations.md

@ -1,45 +1,45 @@
数据库迁移
数据库迁移
==================
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
如源代码,数据库结构演变成为一个数据库驱动程序来进行开发与维护。例如,在开发过程中,可以添加一个新表,or after the application goes live it may be discovered that an additional index is required. It is important to keep track of these structural database changes (called **migration**), just as changes to the source code is tracked using version control. If the source code and the database become out of sync, bugs will occur, or the whole application might break. For this reason, Yii provides a database migration
如源代码,数据库结构演变成为一个数据库驱动程序来进行开发与维护。例如,在开发过程中,可以添加一个新表,or after the application goes live it may be discovered that an additional index is required. It is important to keep track of these structural database changes (called **migration**), just as changes to the source code is tracked using version control. If the source code and the database become out of sync, bugs will occur, or the whole application might break. For this reason, Yii provides a database migration
tool that can keep track of your database migration history, apply new migrations, or revert existing ones.
下列步骤显示了一个团队在开发过程中如何进行 database migration:
下列步骤显示了一个团队在开发过程中如何进行 database migration:
1. Tim 创建了一个新的 migration (如创建一个新表,更改一个列定义,等)。
2. Tim 向版本控制系统提交了一个新的 migration (如 Git, Mercurial)。
3. Doug 从版本控制系统中更新了自己的资料库并接收新的 migration。
4. Doug 将 migration 应用到自己本地开发的数据库,从而同步他的数据库来反映 Tim 所做的更改。
1. Tim 创建了一个新的 migration (如创建一个新表,更改一个列定义,等)。
2. Tim 向版本控制系统提交了一个新的 migration (如 Git, Mercurial)。
3. Doug 从版本控制系统中更新了自己的资料库并接收新的 migration。
4. Doug 将 migration 应用到自己本地开发的数据库,从而同步他的数据库来反映 Tim 所做的更改。
Yii 通过 `yii migrate` 命令行工具来支持 database migration。此工具支持:
Yii 通过 `yii migrate` 命令行工具来支持 database migration。此工具支持:
* 创建新的 migrations
* 创建新的 migrations
* Applying, reverting, and redoing migrations
* Showing migration history and new migrations
Creating Migrations
-------------------
如果想要创建一个新的 migration,运行以下命令:
如果想要创建一个新的 migration,运行以下命令:
```
yii migrate/create <name>
```
The required `name` parameter specifies a very brief description of the migration. 例如,如果 migration 创建一个名为 *news* 的新表,应该使用如下命令:
The required `name` parameter specifies a very brief description of the migration. 例如,如果 migration 创建一个名为 *news* 的新表,应该使用如下命令:
```
yii migrate/create create_news_table
```
As you'll shortly see, the `name` parameter
is used as part of a PHP class name in the migration. 因此,应该只包含字母,
数字和/或下划线。
is used as part of a PHP class name in the migration. 因此,应该只包含字母,
数字和/或下划线。
上面的命令将创建一个
名为 `m101129_185401_create_news_table.php` 的新文件。该文件将被创建在 `@app/migrations` 目录中。起初,migration 文件用下面代码来生成:
上面的命令将创建一个
名为 `m101129_185401_create_news_table.php` 的新文件。该文件将被创建在 `@app/migrations` 目录中。起初,migration 文件用下面代码来生成:
```php
class m101129_185401_create_news_table extends \yii\db\Migration
@ -56,8 +56,8 @@ class m101129_185401_create_news_table extends \yii\db\Migration
}
```
注意和类名相同的文件名,并且遵循
`m<timestamp>_<name>` 模式,where:
注意和类名相同的文件名,并且遵循
`m<timestamp>_<name>` 模式,where:
* `<timestamp>` refers to the UTC timestamp (in the
format of `yymmdd_hhmmss`) when the migration is created,
@ -66,13 +66,13 @@ format of `yymmdd_hhmmss`) when the migration is created,
In the class, the `up()` method should contain the code implementing the actual database
migration. In other words, the `up()` method executes code that actually changes the database. The `down()` method may contain code that reverts the changes made by `up()`.
Sometimes, it is impossible for the `down()` to undo the database migration. 例如,if the migration deletes
Sometimes, it is impossible for the `down()` to undo the database migration. 例如,if the migration deletes
table rows or an entire table, that data cannot be recovered in the `down()` method. In such
cases, the migration is called irreversible, meaning the database cannot be rolled back to
a previous state. When a migration is irreversible, as in the above generated code, the `down()`
method returns `false` to indicate that the migration cannot be reverted.
作为一个例子,让我们来展示 migration 是如何创建一个新表的。
作为一个例子,让我们来展示 migration 是如何创建一个新表的。
```php
@ -97,8 +97,8 @@ class m101129_185401_create_news_table extends \yii\db\Migration
}
```
基类 [[\yii\db\Migration]] 通过 `db` 属性
展示数据库的链接。You can use it for manipulating data and the schema of a database.
基类 [[\yii\db\Migration]] 通过 `db` 属性
展示数据库的链接。You can use it for manipulating data and the schema of a database.
The column types used in this example are abstract types that will be replaced
by Yii with the corresponding types depending on your database management system.
@ -117,9 +117,9 @@ define column types.
Transactional Migrations
------------------------
当进行复杂的 DB migrations 时,we usually want to make sure that each
当进行复杂的 DB migrations 时,we usually want to make sure that each
migration succeeds or fail as a whole so that the database maintains its
consistency and integrity. 为了实现这一目标,可以应用
consistency and integrity. 为了实现这一目标,可以应用
DB transactions. We use the special methods `safeUp` and `safeDown` for these purposes.
```php
@ -163,25 +163,25 @@ When your code uses more then one query it is recommended to use `safeUp` and `s
Applying Migrations
-------------------
为了应用所有的可用的新 migrations (i.e., make the local database up-to-date),
运行如下命令:
为了应用所有的可用的新 migrations (i.e., make the local database up-to-date),
运行如下命令:
```
yii migrate
```
该命令将显示所有的新的 migrations 列表。如果你确定想要应用
migrations,可以在每一个新的 migration 类中运行 `up()` 方法,一个
接一个,按照类名中的 timestamp 值的顺序。
该命令将显示所有的新的 migrations 列表。如果你确定想要应用
migrations,可以在每一个新的 migration 类中运行 `up()` 方法,一个
接一个,按照类名中的 timestamp 值的顺序。
应用 migration 之后,migration 工具将会在 `migration` 表中
做一个记录。This allows the tool to identify which migrations
应用 migration 之后,migration 工具将会在 `migration` 表中
做一个记录。This allows the tool to identify which migrations
have been applied and which have not. If the `migration` table does not exist,
the tool will automatically create it in the database specified by the `db`
[application component](structure-application-components.md).
有时,我们可能会需要应用一个或几个新 migrations。可以使用
如下命令:
有时,我们可能会需要应用一个或几个新 migrations。可以使用
如下命令:
```
yii migrate/up 3
@ -190,7 +190,7 @@ yii migrate/up 3
This command will apply the next 3 new migrations. Changing the value 3 will allow
us to change the number of migrations to be applied.
可以使用如下命令将数据库迁移成特定版本:
可以使用如下命令将数据库迁移成特定版本:
```
yii migrate/to 101129_185401
@ -206,8 +206,8 @@ migrations applied after it will be reverted (to be described in the next sectio
Reverting Migrations
--------------------
To revert the last migration step or several applied migrations, 可以使用如下
命令:
To revert the last migration step or several applied migrations, 可以使用如下
命令:
```
yii migrate/down [step]
@ -216,7 +216,7 @@ yii migrate/down [step]
where the optional `step` parameter specifies how many migrations to be reverted
back. It defaults to 1, meaning only the last applied migration will be reverted back.
正如我们之前所描述,不是所有的 migrations 可以被恢复。Trying to revert
正如我们之前所描述,不是所有的 migrations 可以被恢复。Trying to revert
such migrations will throw an exception and stop the entire reverting process.
@ -224,7 +224,7 @@ Redoing Migrations
------------------
Redoing migrations means first reverting and then applying the specified migrations.
可以用如下命令来完成:
可以用如下命令来完成:
```
yii migrate/redo [step]
@ -237,8 +237,8 @@ It defaults to 1, which means only the last migration will be redone.
Showing Migration Information
-----------------------------
除了应用和恢复 migrations,migration 工具也可以显示
历史 migration 和要应用的新的 migrations。
除了应用和恢复 migrations,migration 工具也可以显示
历史 migration 和要应用的新的 migrations。
```
yii migrate/history [limit]
@ -248,8 +248,8 @@ yii migrate/new [limit]
where the optional parameter `limit` specifies the number of migrations to be
displayed. If `limit` is not specified, all available migrations will be displayed.
第一个命令是显示已经应用的 migrations,第二个命令
显示还未应用的 migrations 。
第一个命令是显示已经应用的 migrations,第二个命令
显示还未应用的 migrations 。
Modifying Migration History
@ -257,14 +257,14 @@ Modifying Migration History
Sometimes, we may want to modify the migration history to a specific migration
version without actually applying or reverting the relevant migrations. This
often happens when developing a new migration. 我们可以使用如下命令
来实现这一目标。
often happens when developing a new migration. 我们可以使用如下命令
来实现这一目标。
```
yii migrate/mark 101129_185401
```
此命令与 `yii migrate/to` 命令非常相似,except that it only
此命令与 `yii migrate/to` 命令非常相似,except that it only
modifies the migration history table to the specified version without applying
or reverting the migrations.
@ -272,7 +272,7 @@ or reverting the migrations.
Customizing Migration Command
-----------------------------
有几种制定 migration 的命令。
有几种制定 migration 的命令。
### Use Command Line Options
@ -308,9 +308,9 @@ To specify these options, execute the migrate command using the following format
yii migrate/up --option1=value1 --option2=value2 ...
```
例如,if we want to migrate a `forum` module whose migration files
are located within the module's `migrations` directory, 我们可以使用如下
命令:
例如,if we want to migrate a `forum` module whose migration files
are located within the module's `migrations` directory, 我们可以使用如下
命令:
```
yii migrate/up --migrationPath=@app/modules/forum/migrations
@ -320,9 +320,9 @@ yii migrate/up --migrationPath=@app/modules/forum/migrations
### Configure Command Globally
While command line options allow us to configure the migration command
on-the-fly, 有时我们可能需要通过配置命令一劳永逸。
例如,我们可能需要使用不同的表来存储历史 migrations,
或者我们可能需要使用自定义的 migrations 模板。We can do so by modifying
on-the-fly, 有时我们可能需要通过配置命令一劳永逸。
例如,我们可能需要使用不同的表来存储历史 migrations,
或者我们可能需要使用自定义的 migrations 模板。We can do so by modifying
the console application's configuration file like the following,
```php
@ -334,15 +334,15 @@ the console application's configuration file like the following,
]
```
现在如果我们运行 `migrate` 命令,不需要每次都进入命令行
上面的配置也将生效。其它命令选项
也可以用这种方法进行配置。
现在如果我们运行 `migrate` 命令,不需要每次都进入命令行
上面的配置也将生效。其它命令选项
也可以用这种方法进行配置。
### Migrating with Multiple Databases
By default, migrations will be applied to the database specified by the `db` [application component](structure-application-components.md).
You may change it by specifying the `--db` option, 例如,
You may change it by specifying the `--db` option, 例如,
```
yii migrate --db=db2
@ -350,7 +350,7 @@ yii migrate --db=db2
The above command will apply *all* migrations found in the default migration path to the `db2` database.
如果你的应用程序使用多个数据库,it is possible that some migrations should be applied
如果你的应用程序使用多个数据库,it is possible that some migrations should be applied
to one database while some others should be applied to another database. In this case, it is recommended that
you create a base migration class for each different database and override the [[yii\db\Migration::init()]]
method like the following,
@ -364,16 +364,16 @@ public function init()
```
To create a migration that should be applied to a particular database, simply extend from the corresponding
base migration class. 现在如果你运行 `yii migrate` 命令,每一个 migration 将被应用到其相应的数据库中。
base migration class. 现在如果你运行 `yii migrate` 命令,每一个 migration 将被应用到其相应的数据库中。
> 注意:Because each migration uses a hardcoded DB connection, the `--db` option of the `migrate` command will
> 注意:Because each migration uses a hardcoded DB connection, the `--db` option of the `migrate` command will
have no effect. Also note that the migration history will be stored in the default `db` database.
如果你想通过 `--db` 选项更改 DB 链接,你可以采用如下方法
使多个数据库一起工作。
如果你想通过 `--db` 选项更改 DB 链接,你可以采用如下方法
使多个数据库一起工作。
对于每个数据库,添加一个迁移路径,在这里保存所有相关的迁移类。为了应用 migrations,
运行如下命令,
对于每个数据库,添加一个迁移路径,在这里保存所有相关的迁移类。为了应用 migrations,
运行如下命令,
```
yii migrate --migrationPath=@app/migrations/db1 --db=db1
@ -381,4 +381,4 @@ yii migrate --migrationPath=@app/migrations/db2 --db=db2
...
```
> 注意:The above approach stores the migration history in different databases specified via the `--db` option.
> 注意:The above approach stores the migration history in different databases specified via the `--db` option.

4
docs/guide-zh-CN/db-mongodb.md

@ -1,6 +1,6 @@
Mongo DB
========
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
>
> 现在还没有内容。
> 现在还没有内容。

4
docs/guide-zh-CN/db-redis.md

@ -1,6 +1,6 @@
Redis
=====
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
>
> 现在还没有内容。
> 现在还没有内容。

4
docs/guide-zh-CN/db-sphinx.md

@ -1,6 +1,6 @@
Sphinx Search
=============
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
>
> 现在还没有内容。
> 现在还没有内容。

30
docs/guide-zh-CN/helper-overview.md

@ -1,13 +1,13 @@
Helpers
=======
> 注意:这部分正在开发中。
> 注意:这部分正在开发中。
Yii 提供许多类来简化常见编码,如对字条串或数组的操作,
HTML 代码生成,等等。这些助手类被编写在命名空间 `yii\helpers` 下,并且
全是静态类 (就是说它们只包含静态属性和静态方法,而且不能实例化)。
Yii 提供许多类来简化常见编码,如对字条串或数组的操作,
HTML 代码生成,等等。这些助手类被编写在命名空间 `yii\helpers` 下,并且
全是静态类 (就是说它们只包含静态属性和静态方法,而且不能实例化)。
可以通过调用其中一个静态方法来使用助手类,如下:
可以通过调用其中一个静态方法来使用助手类,如下:
```php
use yii\helpers\Html;
@ -15,15 +15,15 @@ use yii\helpers\Html;
echo Html::encode('Test > test');
```
> 注意:为了支持 [customizing helper classes](#customizing-helper-classes),Yii 将每一个助手类
分隔成两个类:一个基类 (e.g. `BaseArrayHelper`) 和一个 concrete 类 (e.g. `ArrayHelper`).
当使用助手类时,应该仅使用 concrete 类版本而不使用基类。
> 注意:为了支持 [customizing helper classes](#customizing-helper-classes),Yii 将每一个助手类
分隔成两个类:一个基类 (e.g. `BaseArrayHelper`) 和一个 concrete 类 (e.g. `ArrayHelper`).
当使用助手类时,应该仅使用 concrete 类版本而不使用基类。
Core Helper Classes
-------------------
Yii 发布版中提供以下核心助手类:
Yii 发布版中提供以下核心助手类:
- [ArrayHelper](helper-array.md)
- Console
@ -45,11 +45,11 @@ Customizing Helper Classes <span id="customizing-helper-classes"></span>
To customize a core helper class (e.g. [[yii\helpers\ArrayHelper]]), you should create a new class extending
from the helpers corresponding base class (e.g. [[yii\helpers\BaseArrayHelper]]) and name your class the same
as the corresponding concrete class (e.g. [[yii\helpers\ArrayHelper]]), 包括它的命名空间。This class
as the corresponding concrete class (e.g. [[yii\helpers\ArrayHelper]]), 包括它的命名空间。This class
will then be set up to replace the original implementation of the framework.
下面示例显示了如何自定义 [[yii\helpers\ArrayHelper]] 类的
[[yii\helpers\ArrayHelper::merge()|merge()]] 方法:
下面示例显示了如何自定义 [[yii\helpers\ArrayHelper]] 类的
[[yii\helpers\ArrayHelper::merge()|merge()]] 方法:
```php
<?php
@ -65,7 +65,7 @@ class ArrayHelper extends BaseArrayHelper
}
```
将你的类保存在一个名为 `ArrayHelper.php` 的文件中。该文件可以在任何目录,例如 `@app/components`
将你的类保存在一个名为 `ArrayHelper.php` 的文件中。该文件可以在任何目录,例如 `@app/components`
Next, in your application's [entry script](structure-entry-scripts.md), add the following line of code
after including the `yii.php` file to tell the [Yii class autoloader](concept-autoloading.md) to load your custom
@ -76,5 +76,5 @@ Yii::$classMap['yii\helpers\ArrayHelper'] = '@app/components/ArrayHelper.php';
```
Note that customizing of helper classes is only useful if you want to change the behavior of an existing function
of the helpers. 如果你想为你的应用程序添加附加功能,最好为它创建一个单独的
助手类。
of the helpers. 如果你想为你的应用程序添加附加功能,最好为它创建一个单独的
助手类。

40
docs/guide-zh-CN/helper-url.md

@ -1,13 +1,13 @@
Url Helper
==========
Url 助手提供了一组静态方法来管理 URLs。
Url 助手提供了一组静态方法来管理 URLs。
## Getting Common URLs <span id="getting-common-urls"></span>
这有两种方法你可以获得公共 URLs:home URL 和当前请求的 base URL 。按如下方法可以获得
home URL :
这有两种方法你可以获得公共 URLs:home URL 和当前请求的 base URL 。按如下方法可以获得
home URL :
```php
$relativeHomeUrl = Url::home();
@ -15,8 +15,8 @@ $absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home('https');
```
如果没有参数传递,生成的 URL 是相对路径。你即可以通过 `true` 获取当前 URL 的
schema 也可以指定用 (`https`, `http`) 中的哪个。
如果没有参数传递,生成的 URL 是相对路径。你即可以通过 `true` 获取当前 URL 的
schema 也可以指定用 (`https`, `http`) 中的哪个。
To get base URL of the current request use the following:
@ -31,22 +31,22 @@ The only parameter of the method works exactly the same as for `Url::home()`.
## Creating URLs <span id="creating-urls"></span>
In order to create URL to a given route use `Url::toRoute()` method. 此方法使用 [[\yii\web\UrlManager]] 来创建
一个 URL:
In order to create URL to a given route use `Url::toRoute()` method. 此方法使用 [[\yii\web\UrlManager]] 来创建
一个 URL:
```php
$url = Url::toRoute(['product/view', 'id' => 42]);
```
You may specify the route as a string, e.g., `site/index`. You may also use an array if you want to specify additional
query parameters for the URL being created. 数组的格式必须按如下方式:
query parameters for the URL being created. 数组的格式必须按如下方式:
```php
// generates: /index.php?r=site/index&param1=value1&param2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']
```
如果你想用锚创建一个 URL ,可以使用 `#` 作为数组的一个参数。例如,
如果你想用锚创建一个 URL ,可以使用 `#` 作为数组的一个参数。例如,
```php
// generates: /index.php?r=site/index&param1=value1#name
@ -62,11 +62,11 @@ route has none (e.g. `site/index` or `index`). A relative route will be converte
- If the route has no leading slash (e.g. `site/index`), it is considered to be a route relative to the current module
and will be prepended with the module's [[\yii\base\Module::uniqueId|uniqueId]].
从版本 2.0.2 开始,you may specify a route in terms of an [alias](concept-aliases.md). 如果是这种情况,
从版本 2.0.2 开始,you may specify a route in terms of an [alias](concept-aliases.md). 如果是这种情况,
the alias will first be converted into the actual route which will then be turned into an absolute route according
to the above rules.
下面是一些使用该方法的例子:
下面是一些使用该方法的例子:
```php
// /index.php?r=site/index
@ -85,10 +85,10 @@ echo Url::toRoute('site/index', true);
echo Url::toRoute('site/index', 'https');
```
有另一个方法 `Url::to()` 和 [[toRoute()]] 方法非常相似。The only difference is that this method
requires a route to be specified as an array only. 如果给出一个字符串,将被视为一个 URL。
有另一个方法 `Url::to()` 和 [[toRoute()]] 方法非常相似。The only difference is that this method
requires a route to be specified as an array only. 如果给出一个字符串,将被视为一个 URL。
第一个参数可能是:
第一个参数可能是:
- an array: [[toRoute()]] will be called to generate the URL. For example:
`['site/index']`, `['post/index', 'page' => 2]`. Please refer to [[toRoute()]] for more details
@ -102,7 +102,7 @@ When `$scheme` is specified (either a string or true), an absolute URL with host
[[\yii\web\UrlManager::hostInfo]]) will be returned. If `$url` is already an absolute URL, its scheme
will be replaced with the specified one.
下面是一些用法的示例:
下面是一些用法的示例:
```php
// /index.php?r=site/index
@ -130,9 +130,9 @@ echo Url::to('@web/images/logo.gif', true);
echo Url::to('@web/images/logo.gif', 'https');
```
从版本 2.0.3 开始,you may use [[yii\helpers\Url::current()]] to create a URL based on the currently
从版本 2.0.3 开始,you may use [[yii\helpers\Url::current()]] to create a URL based on the currently
requested route and GET parameters. You may modify or remove some of the GET parameters or add new ones by
passing a `$params` parameter to the method. 例如,
passing a `$params` parameter to the method. 例如,
```php
// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
@ -150,7 +150,7 @@ echo Url::current(['id' => 100]);
## Remember URLs <span id="remember-urls"></span>
There are cases when you need to remember URL and afterwards use it during processing of the one of sequential requests.
它可以通过以下方式来实现:
它可以通过以下方式来实现:
```php
// Remember current URL
@ -170,9 +170,9 @@ $url = Url::previous();
$productUrl = Url::previous('product');
```
## 检查相对 URLs <span id="checking-relative-urls"></span>
## 检查相对 URLs <span id="checking-relative-urls"></span>
To find out if URL is relative i.e. it doesn't have host info part, 你可以使用如下代码:
To find out if URL is relative i.e. it doesn't have host info part, 你可以使用如下代码:
```php
$isRelative = Url::isRelative('test/it');

200
docs/guide-zh-CN/structure-extensions.md

@ -1,35 +1,35 @@
扩展
扩展
==========
扩展是可再发行的软件包,专门设计用在Yii应用中,并具有可随时调用的特点。例如,
在应用的每个页面底部为 [yiisoft/yii2-debug](tool-debugger.md) 扩展增加一个方便调试的工具栏
这会使得你更加容易掌握这些页面是如何生成的。你可以运用扩展模块来加速你的开发进程你也可以将代码打包成扩展文件形式,
来与其他人共享你的工作成果.
扩展是可再发行的软件包,专门设计用在Yii应用中,并具有可随时调用的特点。例如,
在应用的每个页面底部为 [yiisoft/yii2-debug](tool-debugger.md) 扩展增加一个方便调试的工具栏
这会使得你更加容易掌握这些页面是如何生成的。你可以运用扩展模块来加速你的开发进程你也可以将代码打包成扩展文件形式,
来与其他人共享你的工作成果.
> 提示信息: 我们用术语 "extension" 来特指 Yii 特定软件包。对于那些无需 Yii 就能使用的通用软件包
我们会用”包”或”库”这样的术语来表示它们。
> 提示信息: 我们用术语 "extension" 来特指 Yii 特定软件包。对于那些无需 Yii 就能使用的通用软件包
我们会用”包”或”库”这样的术语来表示它们。
## 运用扩展 <a name="using-extensions"></a>
## 运用扩展 <a name="using-extensions"></a>
为了使用扩展,你首先需要安装它。大部分扩展文件会用 [Composer](https://getcomposer.org/)
包来部署,Composer 包可以用以下两个简单的步骤安装:
为了使用扩展,你首先需要安装它。大部分扩展文件会用 [Composer](https://getcomposer.org/)
包来部署,Composer 包可以用以下两个简单的步骤安装:
1. 移除你应用中的 `composer.json` 文件,并指定你要安装的扩展文件 (Composer packages)。
2. 运行 `composer install` 来安装指定的扩展文件。
1. 移除你应用中的 `composer.json` 文件,并指定你要安装的扩展文件 (Composer packages)。
2. 运行 `composer install` 来安装指定的扩展文件。
提示:如果没有的话你需要安装 [Composer](https://getcomposer.org/)。
提示:如果没有的话你需要安装 [Composer](https://getcomposer.org/)。
默认情况下,Composer 安装包注册在 [Packagist](https://packagist.org/) - 最大的开源
Composer 包库。你可以对照着看一下 Packagist 扩展。You may also
默认情况下,Composer 安装包注册在 [Packagist](https://packagist.org/) - 最大的开源
Composer 包库。你可以对照着看一下 Packagist 扩展。You may also
[create your own repository](https://getcomposer.org/doc/05-repositories.md#repository) and configure Composer
to use it. This is useful if you are developing closed open extensions and want to share within your projects.
Extensions installed by Composer are stored in the `BasePath/vendor` directory, where `BasePath` refers to the
application's [base path](structure-applications.md#basePath). Because Composer is a dependency manager, 如果
安装一个包,也要安装它所有的附属包。
application's [base path](structure-applications.md#basePath). Because Composer is a dependency manager, 如果
安装一个包,也要安装它所有的附属包。
例如,为了安装 `yiisoft/yii2-imagine` 扩展,按如下方式修改 `composer.json` 文件:
例如,为了安装 `yiisoft/yii2-imagine` 扩展,按如下方式修改 `composer.json` 文件:
```json
{
@ -43,15 +43,15 @@ application's [base path](structure-applications.md#basePath). Because Composer
}
```
安装完成后,需要查看 `BasePath/vendor` 下的 `yiisoft/yii2-imagine` 目录。同时需要查看
包含安装附属包的 `imagine/imagine` 目录。
安装完成后,需要查看 `BasePath/vendor` 下的 `yiisoft/yii2-imagine` 目录。同时需要查看
包含安装附属包的 `imagine/imagine` 目录。
> 提示信息:`yiisoft/yii2-imagine` 是由 Yii 团队开发和维护的一个核心扩展。All
> 提示信息:`yiisoft/yii2-imagine` 是由 Yii 团队开发和维护的一个核心扩展。All
core extensions are hosted on [Packagist](https://packagist.org/) and named like `yiisoft/yii2-xyz`, where `xyz`
varies for different extensions.
Now you can use the installed extensions like they are part of your application. 下面的例子展示了
如何运用由 `yiisoft/yii2-imagine` 扩展提供的 `yii\imagine\Image` 类:
Now you can use the installed extensions like they are part of your application. 下面的例子展示了
如何运用由 `yiisoft/yii2-imagine` 扩展提供的 `yii\imagine\Image` 类:
```php
use Yii;
@ -62,20 +62,20 @@ Image::thumbnail('@webroot/img/test-image.jpg', 120, 120)
->save(Yii::getAlias('@runtime/thumb-test-image.jpg'), ['quality' => 50]);
```
> 提示信息:扩展类由 [Yii class autoloader](concept-autoloading.md) 自动加载。
> 提示信息:扩展类由 [Yii class autoloader](concept-autoloading.md) 自动加载。
### 手动安装扩展 <a name="installing-extensions-manually"></a>
### 手动安装扩展 <a name="installing-extensions-manually"></a>
某些特殊情况下,可能需要手动安装部分或全部扩展,而不是依靠 Composer。
方法如下
某些特殊情况下,可能需要手动安装部分或全部扩展,而不是依靠 Composer。
方法如下
1. 下载扩展存档文件并解压到 `vendor` 目录。
2. 如果有的话,安装由扩展提供的类自动加载器。
3. 下载并按照说明安装所有相关扩展文件。
1. 下载扩展存档文件并解压到 `vendor` 目录。
2. 如果有的话,安装由扩展提供的类自动加载器。
3. 下载并按照说明安装所有相关扩展文件。
如果一个扩展没有一个自动载入的类但是遵循 [PSR-4 standard](http://www.php-fig.org/psr/psr-4/),
你可以用yii提供的自动载入类去载入这个扩展类。All you need to do is just to
如果一个扩展没有一个自动载入的类但是遵循 [PSR-4 standard](http://www.php-fig.org/psr/psr-4/),
你可以用yii提供的自动载入类去载入这个扩展类。All you need to do is just to
declare a [root alias](concept-aliases.md#defining-aliases) for the extension root directory. For example,
assuming you have installed an extension in the directory `vendor/mycompany/myext`, and the extension classes
are under the `myext` namespace, then you can include the following code in your application configuration:
@ -89,15 +89,15 @@ are under the `myext` namespace, then you can include the following code in your
```
## 创建扩展 <a name="creating-extensions"></a>
## 创建扩展 <a name="creating-extensions"></a>
如果你想和别人共享自己的代码,可以考虑创建一个扩展。
这个扩展能够容纳任何代码,例如一个助手类,a widget, a module, etc.
如果你想和别人共享自己的代码,可以考虑创建一个扩展。
这个扩展能够容纳任何代码,例如一个助手类,a widget, a module, etc.
It is recommended that you create an extension in terms of a [Composer package](https://getcomposer.org/) so that
it can be more easily installed and used by other users, liked described in the last subsection.
按照下边步骤为 Composer 包创建一个扩展。
按照下边步骤为 Composer 包创建一个扩展。
1. Create a project for your extension and host it on a VCS repository, such as [github.com](https://github.com).
The development and maintenance work about the extension should be done on this repository.
@ -109,9 +109,9 @@ it can be more easily installed and used by other users, liked described in the
### `composer.json` <a name="composer-json"></a>
每一个 Composer package 的根目录包都必须包含一个 `composer.json` 文件。The file contains the metadata about
the package. 可以在 [Composer Manual](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup) 手册内找到有关该文件完整的说明。
下边例子展示了 `composer.json` 文件的 `yiisoft/yii2-imagine` 扩展:
每一个 Composer package 的根目录包都必须包含一个 `composer.json` 文件。The file contains the metadata about
the package. 可以在 [Composer Manual](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup) 手册内找到有关该文件完整的说明。
下边例子展示了 `composer.json` 文件的 `yiisoft/yii2-imagine` 扩展:
```json
{
@ -156,8 +156,8 @@ the package.
#### Package Name <a name="package-name"></a>
每一个 Composer 包的名称中都需要包含一个唯一标识,以便容易识别。
The format of package names is `vendorName/projectName`. 例如,in the package name `yiisoft/yii2-imagine`,
每一个 Composer 包的名称中都需要包含一个唯一标识,以便容易识别。
The format of package names is `vendorName/projectName`. 例如,in the package name `yiisoft/yii2-imagine`,
the vendor name and the project name are `yiisoft` and `yii2-imagine`, respectively.
Do NOT use `yiisoft` as vendor name as it is reserved for use by the Yii core code.
@ -171,15 +171,15 @@ We recommend you prefix `yii2-` to the project name for packages representing Yi
It is important that you specify the package type of your extension as `yii2-extension` so that the package can
be recognized as a Yii extension when being installed.
当用户运行 `composer install` 来安装扩展,`vendor/yiisoft/extensions.php`文件
将被自动更新,包括对新扩展。从这个文件中,可以知道 Yii 应用
安装了哪些扩展 (the information can be accessed via [[yii\base\Application::extensions]].
当用户运行 `composer install` 来安装扩展,`vendor/yiisoft/extensions.php`文件
将被自动更新,包括对新扩展。从这个文件中,可以知道 Yii 应用
安装了哪些扩展 (the information can be accessed via [[yii\base\Application::extensions]].
#### Dependencies <a name="dependencies"></a>
您的扩展依赖于 Yii (of course)。So you should list it (`yiisoft/yii2`) in the `require` entry in `composer.json`.
如果您的扩展依赖于其它扩展或者第三方库,你也要将它们罗列出来。
您的扩展依赖于 Yii (of course)。So you should list it (`yiisoft/yii2`) in the `require` entry in `composer.json`.
如果您的扩展依赖于其它扩展或者第三方库,你也要将它们罗列出来。
Make sure you also list appropriate version constraints (e.g. `1.*`, `@stable`) for each dependent package. Use stable
dependencies when your extension is released in a stable version.
@ -197,19 +197,19 @@ simply list the dependency in `composer.json` like the following:
}
```
上面的代码中指出此扩展依赖于 `jquery` Bower package。一般来说,you can use
上面的代码中指出此扩展依赖于 `jquery` Bower package。一般来说,you can use
`bower-asset/PackageName` to refer to a Bower package in `composer.json`, and use `npm-asset/PackageName`
to refer to a NPM package. When Composer installs a Bower or NPM package, by default the package content will be
installed under the `@vendor/bower/PackageName` and `@vendor/npm/Packages` directories, respectively.
These two directories can also be referred to using the shorter aliases `@bower/PackageName` and `@npm/PackageName`.
更多关于资源管理的细节,请参考 [Assets](structure-assets.md#bower-npm-assets) 部分。
更多关于资源管理的细节,请参考 [Assets](structure-assets.md#bower-npm-assets) 部分。
#### Class Autoloading <a name="class-autoloading"></a>
In order for your classes to be autoloaded by the Yii class autoloader or the Composer class autoloader,
you should specify the `autoload` entry in the `composer.json` file, 如下:
you should specify the `autoload` entry in the `composer.json` file, 如下:
```json
{
@ -225,9 +225,9 @@ you should specify the `autoload` entry in the `composer.json` file,
You may list one or multiple root namespaces and their corresponding file paths.
当在一个应用中安装一个扩展时,Yii will create for each listed root namespace
当在一个应用中安装一个扩展时,Yii will create for each listed root namespace
an [alias](concept-aliases.md#extension-aliases) that refers to the directory corresponding to the namespace.
例如,the above `autoload` declaration will correspond to an alias named `@yii/imagine`.
例如,the above `autoload` declaration will correspond to an alias named `@yii/imagine`.
### Recommended Practices <a name="recommended-practices"></a>
@ -236,9 +236,9 @@ Because extensions are meant to be used by other people, you often need to take
we introduce some common and recommended practices in creating high quality extensions.
#### 命名空间 <a name="namespaces"></a>
#### 命名空间 <a name="namespaces"></a>
为了避免名字冲突和使得扩展里的类能自动加载,you should use namespaces and
为了避免名字冲突和使得扩展里的类能自动加载,you should use namespaces and
name the classes in your extension by following the [PSR-4 standard](http://www.php-fig.org/psr/psr-4/) or
[PSR-0 standard](http://www.php-fig.org/psr/psr-0/).
@ -246,18 +246,18 @@ You class namespaces should start with `vendorName\extensionName`, where `extens
in the package name except that it should not contain the `yii2-` prefix. For example, for the `yiisoft/yii2-imagine`
extension, we use `yii\imagine` as the namespace its classes.
不要将 `yii`, `yii2` or `yiisoft` 作为 vendor 名称。这些名称是保留给 Yii 核心代码使用的。
不要将 `yii`, `yii2` or `yiisoft` 作为 vendor 名称。这些名称是保留给 Yii 核心代码使用的。
#### Bootstrapping Classes <a name="bootstrapping-classes"></a>
某些时候,you may want your extension to execute some code during the [bootstrapping process](runtime-bootstrapping.md)
stage of an application. 例如,your extension may want to respond to the application's `beginRequest` event
某些时候,you may want your extension to execute some code during the [bootstrapping process](runtime-bootstrapping.md)
stage of an application. 例如,your extension may want to respond to the application's `beginRequest` event
to adjust some environment settings. While you can instruct users of the extension to explicitly attach your event
handler in the extension to the `beginRequest` event, 最好能够将这个过程自动化。
handler in the extension to the `beginRequest` event, 最好能够将这个过程自动化。
为了实现这一目标,you can create a so-called *bootstrapping class* by implementing [[yii\base\BootstrapInterface]].
例如,
为了实现这一目标,you can create a so-called *bootstrapping class* by implementing [[yii\base\BootstrapInterface]].
例如,
```php
namespace myname\mywidget;
@ -288,29 +288,29 @@ You then list this class in the `composer.json` file of your extension like foll
}
```
当在一个应用中安装一个扩展时,Yii will automatically instantiate the bootstrapping class
当在一个应用中安装一个扩展时,Yii will automatically instantiate the bootstrapping class
and call its [[yii\base\BootstrapInterface::bootstrap()|bootstrap()]] method during the bootstrapping process for
every request.
#### Working with Databases <a name="working-with-databases"></a>
你的扩展可能需要访问数据库。Do not assume that the applications that use your extension will always
use `Yii::$db` as the DB connection. 反而,you should declare a `db` property for the classes that require DB access.
你的扩展可能需要访问数据库。Do not assume that the applications that use your extension will always
use `Yii::$db` as the DB connection. 反而,you should declare a `db` property for the classes that require DB access.
The property will allow users of your extension to customize which DB connection they would like your extension to use.
举个例子,you may refer to the [[yii\caching\DbCache]] class and see how it declares and uses the `db` property.
举个例子,you may refer to the [[yii\caching\DbCache]] class and see how it declares and uses the `db` property.
如果你的扩展需要创建一个详细的 DB tables 或者 要改变 DB schema,需要
如果你的扩展需要创建一个详细的 DB tables 或者 要改变 DB schema,需要
- 提供 [migrations](db-migrations.md) 来操控 DB schema,而不是使用普通的 SQL 文件;
- 尝试让迁移能够适应不同的 DBMS;
- 避免使用 migrations 中的 [Active Record](db-active-record.md)。
- 提供 [migrations](db-migrations.md) 来操控 DB schema,而不是使用普通的 SQL 文件;
- 尝试让迁移能够适应不同的 DBMS;
- 避免使用 migrations 中的 [Active Record](db-active-record.md)。
#### Using Assets <a name="using-assets"></a>
如果你的扩展是 a widget 或者 a module,chances are that it may require some [assets](structure-assets.md) to work.
例如,可能会显示一些包含 images, JavaScript, and CSS 的页面。Because the files of an
如果你的扩展是 a widget 或者 a module,chances are that it may require some [assets](structure-assets.md) to work.
例如,可能会显示一些包含 images, JavaScript, and CSS 的页面。Because the files of an
extension are all under the same directory which is not Web accessible when installed in an application, you have
two choices to make the asset files directly accessible via Web:
@ -324,7 +324,7 @@ Please refer to the [Assets](structure-assets.md) section for more details about
#### Internationalization and Localization <a name="i18n-l10n"></a>
Your extension may be used by applications supporting different languages! 因此,if your extension displays
Your extension may be used by applications supporting different languages! 因此,if your extension displays
content to end users, you should try to [internationalize and localize](tutorial-i18n.md) it. In particular,
- If the extension displays messages intended for end users, the messages should be wrapped into `Yii::t()`
@ -333,66 +333,66 @@ content to end users, you should try to [internationalize and localize](tutorial
- If the extension displays numbers, dates, etc., they should be formatted using [[yii\i18n\Formatter]] with
appropriate formatting rules.
了解更多细节,请参考 [Internationalization](tutorial-i18n.md) 部分的内容。
了解更多细节,请参考 [Internationalization](tutorial-i18n.md) 部分的内容。
#### 测试 <a name="testing"></a>
#### 测试 <a name="testing"></a>
如果你想要让你的扩展没有后顾之忧地完美运行。为了达到这个目的,你应该
在发布扩展前先测试它。
如果你想要让你的扩展没有后顾之忧地完美运行。为了达到这个目的,你应该
在发布扩展前先测试它。
It is recommended that you create various test cases to cover your extension code rather than relying on manual tests.
每次在你发布新版本的扩展之前,你应该简单地运行这些测试案例
everything is in good shape. Yii 提供了测试支持,可以帮你更容易写单元测试,
验收测试和功能测试。更多详细内容,请参考 [Testing](test-overview.md) 部分。
每次在你发布新版本的扩展之前,你应该简单地运行这些测试案例
everything is in good shape. Yii 提供了测试支持,可以帮你更容易写单元测试,
验收测试和功能测试。更多详细内容,请参考 [Testing](test-overview.md) 部分。
#### Versioning <a name="versioning"></a>
你应该给每个扩展的发布配置一个版本号 (e.g. `1.0.1`)。We recommend you follow the
你应该给每个扩展的发布配置一个版本号 (e.g. `1.0.1`)。We recommend you follow the
[semantic versioning](http://semver.org) practice when determining what version numbers should be used.
#### Releasing <a name="releasing"></a>
为了让其他人知道你的版本,你需要将它公之于众。
为了让其他人知道你的版本,你需要将它公之于众。
如果这是你第一次发布一个扩展,你应该先在 Composer 库里注册,比如
[Packagist](https://packagist.org/)。 After that, all you need to do is simply creating a release tag (e.g. `v1.0.1`)
如果这是你第一次发布一个扩展,你应该先在 Composer 库里注册,比如
[Packagist](https://packagist.org/)。 After that, all you need to do is simply creating a release tag (e.g. `v1.0.1`)
on the VCS repository of your extension and notify the Composer repository about the new release. People will
then be able to find the new release, and install or update the extension through the Composer repository.
在你扩展的版本中,除了代码文件外你还需要注意以下几点
来帮助其它人了解和使用你的扩展:
在你扩展的版本中,除了代码文件外你还需要注意以下几点
来帮助其它人了解和使用你的扩展:
* 在包根目录里的 readme 文件:它描述了你的扩展功能以及如何安装和使用.
* 在包根目录里的 readme 文件:它描述了你的扩展功能以及如何安装和使用.
We recommend you write it in [Markdown](http://daringfireball.net/projects/markdown/) format and name the file
as `readme.md`.
* 在包根目录中更新日志文件:它列出了每个版本所做的变化。文件
可以被写成 Markdown 格式并命名为 `changelog.md`
* 在包根目录下升级文件:它提供了有关如何从旧版本升级
的说明。该文件可以被写成 Markdown 格式并命名为 `upgrade.md`
* 教程,demos, screenshots, etc.: these are needed if your extension provides many features that cannot be
* 在包根目录中更新日志文件:它列出了每个版本所做的变化。文件
可以被写成 Markdown 格式并命名为 `changelog.md`
* 在包根目录下升级文件:它提供了有关如何从旧版本升级
的说明。该文件可以被写成 Markdown 格式并命名为 `upgrade.md`
* 教程,demos, screenshots, etc.: these are needed if your extension provides many features that cannot be
fully covered in the readme file.
* API documentation:你的代码应该好好地记录下来。
你可以参考 [Object class file](https://github.com/yiisoft/yii2/blob/master/framework/base/Object.php)
来学习如何记录代码。
* API documentation:你的代码应该好好地记录下来。
你可以参考 [Object class file](https://github.com/yiisoft/yii2/blob/master/framework/base/Object.php)
来学习如何记录代码。
> 提示信息:你代码中的注释可以写成 Markdown 格式。`yiisoft/yii2-apidoc` 扩展为你提供了一
个基于代码注释生成的完美 API 文档工具。
> 提示信息:你代码中的注释可以写成 Markdown 格式。`yiisoft/yii2-apidoc` 扩展为你提供了一
个基于代码注释生成的完美 API 文档工具。
> 提示信息:虽然不是必须的,我们仍建议你的应保持统一的代码风格。可以
参考 [core framework code style](https://github.com/yiisoft/yii2/wiki/Core-framework-code-style)。
> 提示信息:虽然不是必须的,我们仍建议你的应保持统一的代码风格。可以
参考 [core framework code style](https://github.com/yiisoft/yii2/wiki/Core-framework-code-style)。
## Core Extensions <a name="core-extensions"></a>
Yii 提供了以下核心扩展,这些扩展是由 Yii 开发团队维护和开发。它们都是在
[Packagist](https://packagist.org/) 注册的并且可以很容易的被安装在
[Using Extensions](#using-extensions) 部分中。
Yii 提供了以下核心扩展,这些扩展是由 Yii 开发团队维护和开发。它们都是在
[Packagist](https://packagist.org/) 注册的并且可以很容易的被安装在
[Using Extensions](#using-extensions) 部分中。
- [yiisoft/yii2-apidoc](https://github.com/yiisoft/yii2-apidoc):
提供一个可扩展并且是高性能的 API 文档生成器。It is also used to generate the core
提供一个可扩展并且是高性能的 API 文档生成器。It is also used to generate the core
framework API documentation.
- [yiisoft/yii2-authclient](https://github.com/yiisoft/yii2-authclient):
provides a set of commonly used auth clients, such as Facebook OAuth2 client, GitHub OAuth2 client.

Loading…
Cancel
Save