Creating An Api

Ok, I know this may seem like a question with many different answers, but I want to get some idea as to how to proceed.

I’m developing an API. The API will receive and send XML. The message types and how to respond are defined by a value inside the XML. So, when I receive an XML post, I will parse through the XML, and find the appropriate response.

I would like to set up the API so that the clients only need to post to one page. (i.e. example.com/api/). My system will then determine the type of XML request, and respond appropriately.

My question is, (I’m new to MVC, so please excuse the ignorance), would I just route every request to one controller that will interpret the XML and then execute the appropriate code? If so, how do I adjust the urlManager to route all post requests to the ApiController class? Or would I use a routes.php for this?

Let me know if this doesn’t make sense and I will try and clarify. Thanks in advance.

You need to set url in config/main.php (or based on your settings) under UrlManager

Actually, Yii automatically uses default url as index.php?r=api (in your current case) and index.php?r=api/getpost in case you have an action getPost in api controller.

I think you should create a API controller with different actions for the different type of API requests (i.e. post/update/delete etc.). So your client can send a post request to index.php?r=api/post (or api/post), where ‘api’ is your controller. You process all data in the ‘post’ action and give back a proper response (in any format you want off course).

A little more info about the MVC design pattern can be found here: http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc

You can set more friendly URL’s in config/main.php like PeRoChAk sais, more info about that can be found here:

http://www.yiiframework.com/doc/guide/1.1/en/topics.url