This means that you need to create an application such as frontend or backend ( Yii 2 extended application template ), what you need to do is create another api directory call, the same as backend or frontend, and it will contain the folder structure same as backend | frontend, except for assets, views, widgets, etc.
Basically you need a folder structure like this
api -config -modules --v1 ---controllers ---models -runtime -tests -web backend common console environments frontend
If you are going to use the Yii 2 basic application template to create a rest api, this is possible. create an api module call and create a v1 subdirectory as a submodule. (The Yii doc -A module may consist of submodules.) (GiovanniDerks - backend sub-modules)
-modules --api ---v1 ----controllers ----models
There is an advantage to using one of these folder structures because you donβt have to worry much about the route.
https://domain.com/api/v1/products
Here is a good example for a RESTful API with a preliminary template
Install RESTful API in Yii2 (budiirawan)
API and RESTFull API are different. The RESTFull API must have REST standards. mainly why APIs are developed as a standalone application. in a regular application, we create 4 actions for CRUD functions. but in the yii2 RESTFull API we just create one action for all CRUD functions. (Controllers expand from the active REST controller - yii \ rest \ ActiveController). in the kernel code you can find 4 actions for different headers GET, POST, PUT and DELETE.
'index' => ['GET', 'HEAD'], 'view' => ['GET', 'HEAD'], 'create' => ['POST'], 'update' => ['PUT', 'PATCH'], 'delete' => ['DELETE'],
for authentication we can mainly use "HTTP Basic Authentication"