T-DOC
  • Laravel-11
  • Laravel-10
  • Laravel-6X
  • Laravel-5.5
Dcat-Admin
  • Yii2
  • 重构2
  • Laravel-11
  • Laravel-10
  • Laravel-6X
  • Laravel-5.5
Dcat-Admin
  • Yii2
  • 重构2
  • 前言

    • 关于 Yii
    • 从 Yii 1.1 升级
    • Yii 2.0 升级说明
    • 贡献者指引
    • 捐献和赞助
  • 版本管理

    • 发行说明
    • 版本说明
    • 更新记录
  • 快速入门

    • 安装 Yii
    • 目录结构
    • 部署
    • Hello world
    • 使用表单
    • 使用数据库
    • 使用 Gii 生成代码
    • 编辑器与 IDE
  • 核心架构

    • 请求生命周期
    • 单一入口
    • 应用主体
    • 组件
    • 属性
    • 事件
    • 行为
    • 配置
    • 别名
    • 类自动加载
    • 服务定位器
    • 依赖注入容器
  • 高级应用模板

    • 安装
    • 框架结构
    • 运行测试
    • 环境与配置
    • 自定义应用模板
  • 基础功能

    • 路由
    • 控制器
    • 过滤器
    • 请求
    • 响应
    • 模型
    • 视图
    • 模块
    • 小部件
    • 前端资源
    • 扩展
    • Sessions 和 Cookies
    • 错误处理
    • 日志
  • 数据库

    • 数据库访问对象(DAO)
    • 查询构造器
    • 活动记录
    • 数据库迁移
    • Redis
    • Sphinx
    • Elasticsearch
    • MongoDB
  • 安全

    • 认证
    • 授权
    • 加密
    • Email 认证
    • 验证码
    • 社会化登录
    • 密码
  • 进阶功能

    • 缓存
    • 发送邮件
    • HTTP 客户端
    • 国际化
    • 队列
    • 控制台应用
    • 助手类
    • 验证器
  • RESTful Web 服务

    • 快速入门
    • 资源
    • 控制器
    • 路由
    • 格式化响应
    • 认证
    • 速率限制(限流)
    • 版本化
    • 错误处理
  • 开发工具

    • 调试工具栏和调试器
    • 使用 Gii 生成代码

路由

With resource and controller classes ready, you can access the resources using the URL like http://localhost/index.php?r=user/create, similar to what you can do with normal Web applications.

In practice, you usually want to enable pretty URLs and take advantage of HTTP verbs. For example, a request POST /users would mean accessing the user/create action. This can be done easily by configuring the urlManager application component in the application configuration like the following:

'urlManager' => [
    'enablePrettyUrl' => true,
    'enableStrictParsing' => true,
    'showScriptName' => false,
    'rules' => [
        ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
    ],
]

Compared to the URL management for Web applications, the main new thing above is the use of [[yii\rest\UrlRule]] for routing RESTful API requests. This special URL rule class will create a whole set of child URL rules to support routing and URL creation for the specified controller(s). For example, the above code is roughly equivalent to the following rules:

[
    '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',
]

And the following API endpoints are supported by this rule:

  • GET /users: list all users page by page;
  • HEAD /users: show the overview information of user listing;
  • POST /users: create a new user;
  • GET /users/123: return the details of the user 123;
  • HEAD /users/123: show the overview information of user 123;
  • PATCH /users/123 and PUT /users/123: update the user 123;
  • DELETE /users/123: delete the user 123;
  • OPTIONS /users: show the supported verbs regarding endpoint /users;
  • OPTIONS /users/123: show the supported verbs regarding endpoint /users/123.

You may configure the only and except options to explicitly list which actions to support or which actions should be disabled, respectively. For example,

[
    'class' => 'yii\rest\UrlRule',
    'controller' => 'user',
    'except' => ['delete', 'create', 'update'],
],

You may also configure patterns or extraPatterns to redefine existing patterns or add new patterns supported by this rule. For example, to support a new action search by the endpoint GET /users/search, configure the extraPatterns option as follows,

[
    'class' => 'yii\rest\UrlRule',
    'controller' => 'user',
    'extraPatterns' => [
        'GET search' => 'search',
    ],
]

You may have noticed that the controller ID user appears in plural form as users in the endpoint URLs. This is because [[yii\rest\UrlRule]] automatically pluralizes controller IDs when creating child URL rules. You may disable this behavior by setting [[yii\rest\UrlRule::pluralize]] to be 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.

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.

[
    'class' => 'yii\rest\UrlRule',
    'controller' => ['u' => 'user'],
]

所含规则的额外配置

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:

[
    'class' => 'yii\rest\UrlRule',
    'controller' => ['user'],
    'ruleConfig' => [
        'class' => 'yii\web\UrlRule',
        'defaults' => [
            'expand' => 'profile',
        ]
    ],
],

💖喜欢本文档的,欢迎点赞、收藏、留言或转发,谢谢支持!
作者邮箱:zhuzixian520@126.com,github地址:github.com/zhuzixian520

Last Updated:
Contributors: Hans
Prev
控制器
Next
格式化响应