For buiding restful API using Yii2, does anyone has good example on how to add a new action in a controller? Thanks.
相关问题
- Design RESTful service with multiple ids
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- Laravel 5.1 MethodNotAllowedHttpException on store
- Google places autocomplete suggestion without coun
相关文章
- 我用scrapy写了一个蛮简单的爬虫怎么封装成一个api啊
- 后端给前端的API接口是怎么用代码写的
- Can you run console jobs from yii2-basic?
- Convert C# Object to Json Object
- How to do a bulk database insert in Yii2?
- Android camera2 API get focus distance in AF mode
- Got ActiveRecord::AssociationTypeMismatch on model
- Multiple parameters in AngularJS $resource GET
Here is a good example using Yii 2 advanced application template
https://github.com/deerawan/yii2-advanced-api
more detail of this project 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)
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.
Firstly, the framework includes
\yii\rest\ActiveController
that provides typical restful API operation and URL management.Basically, the controller predefines the CRUD operations as followed:
POST /resource
->actionCreate
-> Create the resourceGET /resource/{id}
->actionView
-> Read the resourcePUT, PATCH /resource/{id}
->actionUpdate
-> Update the resourceDELETE /resource/{id}
->actionDelete
-> Delete the resourceGET /resource
->actionIndex
-> List all the resourcesThe URL routing rules and actions definition can be found in
\yii\rest\ActiveController
,\yii\rest\UrlRule
and the respective\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 underurlManager
:Effectively, this will generate a new routing rule, requesting
POST /resource/{id}/your_preferred_url
will invokeactionXxxxx
of your ResourceController.