Yii2 Restful API - 添加新动作的示例

时间:2022-03-19 19:10:50

For buiding restful API using Yii2, does anyone has good example on how to add a new action in a controller? Thanks.

为了使用Yii2建立restful API,有没有人有如何在控制器中添加新动作的好例子?谢谢。

2 个解决方案

#1


28  

I am not sure if you are asking for extra actions beside CRUD or just for CRUD, so I write in details for both cases.

我不确定你是否要求在CRUD旁边或仅仅为CRUD做额外的动作,所以我会详细说明这两种情况。

Firstly, the framework includes \yii\rest\ActiveController that provides typical restful API operation and URL management.

首先,该框架包括\ yii \ rest \ ActiveController,它提供典型的restful API操作和URL管理。

Basically, the controller predefines the CRUD operations as followed:

基本上,控制器预定义CRUD操作如下:

POST /resource -> actionCreate -> Create the resource

POST / resource - > actionCreate - >创建资源

GET /resource/{id} -> actionView -> Read the resource

GET / resource / {id} - > actionView - >读取资源

PUT, PATCH /resource/{id} -> actionUpdate -> Update the resource

PUT,PATCH / resource / {id} - > actionUpdate - >更新资源

DELETE /resource/{id} -> actionDelete -> Delete the resource

DELETE / resource / {id} - > actionDelete - >删除资源

GET /resource -> actionIndex -> List all the resources

GET / resource - > actionIndex - >列出所有资源

The URL routing rules and actions definition can be found in \yii\rest\ActiveController, \yii\rest\UrlRule and the respective \yii\rest\*Action.

URL路由规则和操作定义可以在\ yii \ rest \ ActiveController,\ yii \ rest \ UrlRule和相应的\ yii \ rest \ * Action中找到。

Secondly, if you want to add extra restful API in the controller, you can simply write your extra actionXxxxx(), and in configuration, add the following url rules under urlManager:

其次,如果你想在控制器中添加额外的restful API,你可以简单地编写额外的actionXxxxx(),并在配置中,在urlManager下添加以下url规则:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => ['resource'],
            'pluralize' => false,
            'extraPatterns' => [
                'POST {id}/your_preferred_url' => 'xxxxx', // 'xxxxx' refers to 'actionXxxxx'
            ],
        ],
    ],
],

Effectively, this will generate a new routing rule, requesting POST /resource/{id}/your_preferred_url will invoke actionXxxxx of your ResourceController.

实际上,这将生成一个新的路由规则,请求POST / resource / {id} / your_preferred_url将调用ResourceController的actionXxxxx。

#2


3  

Here is a good example using Yii 2 advanced application template

这是使用Yii 2高级应用程序模板的一个很好的例子

https://github.com/deerawan/yii2-advanced-api

https://github.com/deerawan/yii2-advanced-api

more detail of this project http://budiirawan.com/setup-restful-api-yii2/

该项目的更多细节http://budiirawan.com/setup-restful-api-yii2/

also you can use Yii 2 basic application template if you want. what you have to do is follow this kind of folder structure (v1 for version) (Yii doc -A module may consist of sub-modules.)(GiovanniDerks - backend sub-modules)

如果需要,您也可以使用Yii 2基本应用程序模板。您需要做的是遵循这种文件夹结构(版本为v1)(Yii doc -A模块可能包含子模块。)(GiovanniDerks - 后端子模块)

-modules
--api
---v1
----controllers
----models

#1


28  

I am not sure if you are asking for extra actions beside CRUD or just for CRUD, so I write in details for both cases.

我不确定你是否要求在CRUD旁边或仅仅为CRUD做额外的动作,所以我会详细说明这两种情况。

Firstly, the framework includes \yii\rest\ActiveController that provides typical restful API operation and URL management.

首先,该框架包括\ yii \ rest \ ActiveController,它提供典型的restful API操作和URL管理。

Basically, the controller predefines the CRUD operations as followed:

基本上,控制器预定义CRUD操作如下:

POST /resource -> actionCreate -> Create the resource

POST / resource - > actionCreate - >创建资源

GET /resource/{id} -> actionView -> Read the resource

GET / resource / {id} - > actionView - >读取资源

PUT, PATCH /resource/{id} -> actionUpdate -> Update the resource

PUT,PATCH / resource / {id} - > actionUpdate - >更新资源

DELETE /resource/{id} -> actionDelete -> Delete the resource

DELETE / resource / {id} - > actionDelete - >删除资源

GET /resource -> actionIndex -> List all the resources

GET / resource - > actionIndex - >列出所有资源

The URL routing rules and actions definition can be found in \yii\rest\ActiveController, \yii\rest\UrlRule and the respective \yii\rest\*Action.

URL路由规则和操作定义可以在\ yii \ rest \ ActiveController,\ yii \ rest \ UrlRule和相应的\ yii \ rest \ * Action中找到。

Secondly, if you want to add extra restful API in the controller, you can simply write your extra actionXxxxx(), and in configuration, add the following url rules under urlManager:

其次,如果你想在控制器中添加额外的restful API,你可以简单地编写额外的actionXxxxx(),并在配置中,在urlManager下添加以下url规则:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        [
            'class' => 'yii\rest\UrlRule',
            'controller' => ['resource'],
            'pluralize' => false,
            'extraPatterns' => [
                'POST {id}/your_preferred_url' => 'xxxxx', // 'xxxxx' refers to 'actionXxxxx'
            ],
        ],
    ],
],

Effectively, this will generate a new routing rule, requesting POST /resource/{id}/your_preferred_url will invoke actionXxxxx of your ResourceController.

实际上,这将生成一个新的路由规则,请求POST / resource / {id} / your_preferred_url将调用ResourceController的actionXxxxx。

#2


3  

Here is a good example using Yii 2 advanced application template

这是使用Yii 2高级应用程序模板的一个很好的例子

https://github.com/deerawan/yii2-advanced-api

https://github.com/deerawan/yii2-advanced-api

more detail of this project http://budiirawan.com/setup-restful-api-yii2/

该项目的更多细节http://budiirawan.com/setup-restful-api-yii2/

also you can use Yii 2 basic application template if you want. what you have to do is follow this kind of folder structure (v1 for version) (Yii doc -A module may consist of sub-modules.)(GiovanniDerks - backend sub-modules)

如果需要,您也可以使用Yii 2基本应用程序模板。您需要做的是遵循这种文件夹结构(版本为v1)(Yii doc -A模块可能包含子模块。)(GiovanniDerks - 后端子模块)

-modules
--api
---v1
----controllers
----models