Class yii\rest\UrlRule
| 继承 | yii\rest\UrlRule » yii\web\CompositeUrlRule » yii\base\BaseObject |
|---|---|
| 实现 | yii\base\Configurable, yii\web\UrlRuleInterface |
| 可用版本自 | 2.0 |
| 源码 | https://github.com/yiichina/yii2/blob/api/framework/rest/UrlRule.php |
UrlRule 用来简化 RESTful API 支持的 URL 规则的创建。
UrlRule 最简单的用法是在应用程序配置中声明如下规则,
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
]
上面的代码将创建一整套支持以下 RESTful API 端点的 URL 规则:
'PUT,PATCH users/<id>' => 'user/update':更新用户'DELETE users/<id>' => 'user/delete':删除用户'GET,HEAD users/<id>' => 'user/view':返回用户的详细信息/概述/选项'POST users' => 'user/create':创建一个新用户'GET,HEAD users' => 'user/index':返回用户的列表/概述/选项'users/<id>' => 'user/options':响应用户的所有未处理动词'users' => 'user/options':响应所有未处理的用户集合动词
您可以配置 $only 或者 $except 以禁用上述某些规则。
您可以配置 $patterns 以完全重新定义自己的规则列表。
您可以配置 $controller 以多个控制器 ID,来生成所有这些控制器的规则。
例如,以下代码将禁用 delete 规则并为 user 和 post 控制器生成规则:
[
'class' => 'yii\rest\UrlRule',
'controller' => ['user', 'post'],
'except' => ['delete'],
]
$controller 属性是必需的,应代表一个或多个控制器ID。
如果控制器位于模块内,则每个控制器ID都应以模块ID为前缀。
使用的控制器ID将自动复数(例如 user 变为 users
如上例所示)。
关于 UrlRule 的更多使用参考,请查看 Rest 路由指南。
公共属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $controller | string|array | 控制器ID(例如 user,post-comment)表示这些规则在此复合规则将被处理
如果控制器在模块内(例如 admin/user),它应该以模块 ID 为前缀。
默认情况下,控制器ID在放入模式时会自动复数以生产规则
如果要明确指定控制器 ID 在模式中的显示方式,
你可以在模式中使用数组,键为模式名,值为控制器实际的 ID,
例如 ['u' => 'user']。
你还可以传递多个控制器 ID 的数组。此时,复合规则将
为每个指定的控制器生成适用的URL规则。例如 ['user', 'post']。 |
yii\rest\UrlRule |
| $createUrlStatus | null|integer | Status of the URL creation after the last createUrl() call. | yii\web\CompositeUrlRule |
| $except | array | 除外的动作(Action)列表,这里的动作都不会创建 URL 规则 | yii\rest\UrlRule |
| $extraPatterns | array | 额外增加的模式,会被加入 $patterns 中, 键为模式名称,值为相应的动作 ID。 这些额外的模式将优先于 $patterns 生效。 | yii\rest\UrlRule |
| $only | array | 接受的动作(Action)列表,如果不为空,仅仅是这数组中的动作 会创建相应的 URL 规则。 | yii\rest\UrlRule |
| $patterns | array | 可用的模式对应相应动作的列表,用以创建 URL 规则。
键为模式,值为相应的动作。
模式的格式是 Verbs Pattern, 其中 Verbs 代表逗号分隔的 HTTP 动词列表(没有空格)。
如果 Verbs 指定,意味着所有动词都被允许。
Pattern 是可选的,它将以 $prefix/$controller/ 为前缀,
其中的标记会被 $tokens 替换。 |
yii\rest\UrlRule |
| $pluralize | boolean | 是否自动复数控制器的 URL名 称。
如果为 true, 则控制器 ID 将以复数形式显示在 URL 中。例如 user 控制器
将在URL中显示为 users 。 |
yii\rest\UrlRule |
| $prefix | string | 每一个模式的公共前缀。 | yii\rest\UrlRule |
| $ruleConfig | array | 用于创建此规则包含的每个 URL 规则的默认配置。 | yii\rest\UrlRule |
| $suffix | string | 后缀,将被赋值给 yii\web\UrlRule::$suffix 以生成的每个规则 | yii\rest\UrlRule |
| $tokens | array | 每个模式替换的标记列表。键为标记名称, 值为相应的替换 | yii\rest\UrlRule |
受保护的属性
| 属性 | 类型 | 描述 | 被定义在 |
|---|---|---|---|
| $createStatus | integer|null | Status of the URL creation after the last createUrl() call. | yii\web\CompositeUrlRule |
| $rules | yii\web\UrlRuleInterface[] | The URL rules contained in this composite rule. | yii\web\CompositeUrlRule |
公共方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| __call() | Calls the named method which is not a class method. | yii\base\BaseObject |
| __construct() | Constructor. | yii\base\BaseObject |
| __get() | Returns the value of an object property. | yii\base\BaseObject |
| __isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
| __set() | Sets value of an object property. | yii\base\BaseObject |
| __unset() | Sets an object property to null. | yii\base\BaseObject |
| canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\BaseObject |
| canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
| className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
| createUrl() | Creates a URL according to the given route and parameters. | yii\rest\UrlRule |
| getCreateUrlStatus() | Returns status of the URL creation after the last createUrl() call. | yii\web\CompositeUrlRule |
| hasMethod() | Returns a value indicating whether a method is defined. | yii\base\BaseObject |
| hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
| init() | Initializes the object. | yii\rest\UrlRule |
| parseRequest() | Parses the given request and returns the corresponding route and parameters. | yii\rest\UrlRule |
受保护的方法
| 方法 | 描述 | 被定义在 |
|---|---|---|
| createRule() | 使用给定的模式和操作创建URL规则。 | yii\rest\UrlRule |
| createRules() | Creates the URL rules that should be contained within this composite rule. | yii\rest\UrlRule |
| iterateRules() | Iterates through specified rules and calls createUrl() for each of them. | yii\web\CompositeUrlRule |
属性详情
控制器ID(例如 user,post-comment)表示这些规则在此复合规则将被处理
如果控制器在模块内(例如 admin/user),它应该以模块 ID 为前缀。
默认情况下,控制器ID在放入模式时会自动复数以生产规则
如果要明确指定控制器 ID 在模式中的显示方式,
你可以在模式中使用数组,键为模式名,值为控制器实际的 ID,
例如 ['u' => 'user']。
你还可以传递多个控制器 ID 的数组。此时,复合规则将
为每个指定的控制器生成适用的URL规则。例如 ['user', 'post']。
除外的动作(Action)列表,这里的动作都不会创建 URL 规则
参见 $patterns.
接受的动作(Action)列表,如果不为空,仅仅是这数组中的动作 会创建相应的 URL 规则。
参见 $patterns.
可用的模式对应相应动作的列表,用以创建 URL 规则。
键为模式,值为相应的动作。
模式的格式是 Verbs Pattern, 其中 Verbs 代表逗号分隔的 HTTP 动词列表(没有空格)。
如果 Verbs 指定,意味着所有动词都被允许。
Pattern 是可选的,它将以 $prefix/$controller/ 为前缀,
其中的标记会被 $tokens 替换。
是否自动复数控制器的 URL名 称。
如果为 true, 则控制器 ID 将以复数形式显示在 URL 中。例如 user 控制器
将在URL中显示为 users 。
参见 $controller.
每一个模式的公共前缀。
用于创建此规则包含的每个 URL 规则的默认配置。
后缀,将被赋值给 yii\web\UrlRule::$suffix 以生成的每个规则
每个模式替换的标记列表。键为标记名称, 值为相应的替换
参见 $patterns.
方法详情
使用给定的模式和操作创建URL规则。
| protected yii\web\UrlRuleInterface createRule($pattern, $prefix, $action) | ||
| $pattern | string | |
| $prefix | string | |
| $action | string | |
Creates the URL rules that should be contained within this composite rule.
| protected yii\web\UrlRuleInterface[] createRules() | ||
| return | yii\web\UrlRuleInterface[] | The URL rules |
|---|---|---|
Creates a URL according to the given route and parameters.
| public string|boolean createUrl($manager, $route, $params) | ||
| $manager | yii\web\UrlManager | The URL manager |
| $route | string | The route. It should not have slashes at the beginning or the end. |
| $params | array | The parameters |
| return | string|boolean | The created URL, or false if this rule cannot be used for creating this URL. |
|---|---|---|
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
| public void init() |
Parses the given request and returns the corresponding route and parameters.
| public array|boolean parseRequest($manager, $request) | ||
| $manager | yii\web\UrlManager | The URL manager |
| $request | yii\web\Request | The request component |
| return | array|boolean | The parsing result. The route and the parameters are returned as an array. If false, it means this rule cannot be used to parse this path info. |
|---|---|---|