JimK
JimK

Reputation: 19

Yii - Adding RESTFull API to webapp

I have an existing app that was built on the Yii Framework for managing wine cellars. I used the standard Gii utility to create the initial CRUD controllers/models/views. I've significantly modified the code to meet my needs for navigation, look & feel and functionality. I'm now beginning the design for a companion mobile (android) app that will need to consume and create some of the data (get a list of wines in my cellar - GET, add a new wine to the cellar - POST) that can be used while out shopping/wine-tasting. I don't intend to expose all the models/controller options as web services, only a specific set of functions.

My question is really a design issue. Should I add the API methods to the existing CRUD controllers or should I create a new "API Controller" for each of the models (or a single apiController is another option I've seen)? My thinking is separate API controllers. This would allow me to update/deploy API specific changes more easily and to logically compartmentalize that interface. This API will need to be authenticated and I will probably implement OAuth in a future release.

BTW, I've looked at the RESTFullYii extension and I haven't been able to grok exactly how it works. I'd really love to see a working example app not just code snippets.

Upvotes: 0

Views: 816

Answers (2)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

I suggest you to create a new controller. For example ApiController. You put all your actions such as actionAddWine. So you can call this action like:

http://example.com/index.php?r=api/addWine

The advantage of doing this, is that you have all your models, and you just need to interact with your modes via REST. In order to create a simple REST api you can check the following document which is really simple explained.

How-To: Create a REST API

If you do like the mentioned article, You can send the response to client simply using _sendResponse() method. Another thing is to do authentications and similar things which can is readily attainable using your controller's init() method.

Another way is to create a module for your application. If you want to completely divide your API from your other codes you can create a module. You can create modules with GII. The advantage of module is that you can separate your sections. For example your actions will be:

http://example.com/index.php?r=api/wine/add

in above url you are calling an action in api module, and wine controller which its name is add.

Note that you can use your models in module by importing them.

Upvotes: 2

acorncom
acorncom

Reputation: 5955

Yes, go for separate controllers. You may even want to set up a separate application that shares some of your apps models and config but also allows for a more separate feel.

Also with planning how you will version your Api, as it is likely to need to change down the road while still needing to support your older Android app

Upvotes: 0

Related Questions