This commit is contained in:
jiangtao
2017-09-28 14:14:22 +08:00
parent a193b06bd8
commit aebcafe04c

View File

@ -2,11 +2,11 @@
=======
随着资源和控制器类准备您可以使用URL如
`http://localhost/index.php?r=user/create`访问资源类似于你可以用正常的Web应用程序做法。
`http://localhost/index.php?r=user/create` 访问资源,类似于你可以用正常的 Web 应用程序做法。
在实践中你通常要用美观的URL并采取有优势的HTTP动词。
例如,请求`POST /users`意味着访问`user/create`动作。
这可以很容易地通过配置`urlManager`应用程序组件来完成
在实践中,你通常要用美观的 URL 并采取有优势的 HTTP 动词。
例如,请求 `POST /users` 意味着访问 `user/create` 动作。
这可以很容易地通过配置 `urlManager` 应用程序组件来完成
如下所示:
```php
@ -20,9 +20,9 @@
]
```
相比于URL管理的Web应用程序上述主要的新东西是通过RESTful API
请求[[yii\rest\UrlRule]]。这个特殊的URL规则类将会
建立一整套子URL规则来支持路由和URL创建的指定的控制器。
相比于 URL 管理的 Web 应用程序,上述主要的新东西是通过 RESTful API
请求 [[yii\rest\UrlRule]] 。这个特殊的 URL 规则类将会
建立一整套子 URL 规则来支持路由和 URL 创建的指定的控制器。
例如, 上面的代码中是大致按照下面的规则:
```php
@ -37,15 +37,15 @@
]
```
该规则支持下面的API末端:
该规则支持下面的 API 末端:
* `GET /users`: 逐页列出所有用户;
* `HEAD /users`: 显示用户列表的概要信息;
* `POST /users`: 创建一个新用户;
* `GET /users/123`: 返回用户为123的详细信息;
* `GET /users/123`: 返回用户为 123 的详细信息;
* `HEAD /users/123`: 显示用户 123 的概述信息;
* `PATCH /users/123` and `PUT /users/123`: 更新用户123;
* `DELETE /users/123`: 删除用户123;
* `PATCH /users/123` and `PUT /users/123`: 更新用户 123;
* `DELETE /users/123`: 删除用户 123;
* `OPTIONS /users`: 显示关于末端 `/users` 支持的动词;
* `OPTIONS /users/123`: 显示有关末端 `/users/123` 支持的动词。
@ -73,16 +73,16 @@
]
```
您可能已经注意到控制器ID`user`以复数形式出现在`users`末端。这是因为 [[yii\rest\UrlRule]] 能够为他们使用的末端全自动复数化控制器ID
您可以通过设置 [[yii\rest\UrlRule::pluralize]] 为false 来禁用此行为,如果您想
使用一些特殊的名字您可以通过置 [[yii\rest\UrlRule::controller]] 属性
您可能已经注意到控制器ID `user` 以复数形式出现在 `users` 末端。
这是因为 [[yii\rest\UrlRule]] 能够为他们使用的末端全自动复数化控制器 ID。
您可以通过置 [[yii\rest\UrlRule::pluralize]] 为 false 来禁用此行为
> Info: The pluralization of controller IDs is done by [[yii\helpers\Inflector::pluralize()]]. The method respects
special pluralization rules. For example, the word `box` will be pluralized as `boxes` instead of `boxs`.
> 信息:控制器的 ID 复数化由 [[yii\helpers\Inflector::pluralize()]] 完成。该方法遵循
特定的规则. 举个例子, 单词 `box` 会被复数化为 `boxes` 而不是 `boxs`
In case when the automatic pluralization does not meet your requirement, you may also configure the
[[yii\rest\UrlRule::controller]] property to explicitly specify how to map a name used in endpoint URLs to
a controller ID. For example, the following code maps the name `u` to the controller ID `user`.
如果自动复数化不能满足你的需求,你也可以配置
[[yii\rest\UrlRule::controller]] 属性来明确指定如何将端点URL中使用的名称映射到
控制器ID。例如以下代码将名称 `u` 映射到控制器ID `user`
```php
[
@ -91,10 +91,10 @@ a controller ID. For example, the following code maps the name `u` to the contro
]
```
## Extra configuration for contained rules
## 包含规则的额外配置
It could be useful to specify extra configuration that is applied to each rule contained within [[yii\rest\UrlRule]].
A good example would be specifying defaults for `expand` parameter:
在 [[yii\rest\UrlRule]] 中所包含的每个规则中,指定额外的配置可能很有用。
一个很好的例子就是指定 `expand` 参数的默认值:
```php
[