Search code examples
phpyii2

Yii2 Rest API Location


I needed to know which folder is more suitable to host my Rest API in Yii2 template advanced, backend or frontend. I also saw some examples where developers created a new directory, called it "api" and programmed the API from there, but I don't find much information by developing from this method. If you know the most correct way to do it, I would appreciate if you could explain me.


Solution

  • That totally depends on your requirements if it is going to be a complete full app or just a single controller with a few actions or a single ActiveController.

    But a good way is to keep things separate just like you are using the advance-app and you have the backend and frontend folders so that you can differentiate the code base, and keep the code in their concerned folders, those folders are actually 2 separate applications running separately with their own separate configurations i.e backend/config/* and frontend/config/*, and can share some common configurations like database and others from the common/config/*. Same like that you can create a separate application/folder where you can have all the code base and configurations related to api you are going to build.

    You can find a very good reference for adding more apps into your existing application by Yii here on GITHUB which will clear your concept about how to add more apps.

    Then you can go to this article by Budi Irawan's Tech Blog which uses the advance-app to add a new application for the API. You will find the directory structure with example models and controllers along with testing them with POSTMAN.