Create REST Web Service

How do you create a REST based web service using YII? I’ve seen the question asked but no answers nor can i find any tutorials anywhere?


Applications written with the Yii Framework will likely have RESTful qualities. The thought behind REST is that a concise and uniform vocabulary is provided to a client. Yii applications typically return HTML messages, in which case the client is a browser. You may be interested in returning other internet media types like JSON or XML for consumption by other applications. Check out the URI management capabilities that Yii offers:

Yii URI Management

A great step toward designing your RESTful application is planning the structure of your requests. The format of your responses will certainly depend on what is consuming the service.


If you’re able to use SOAP, here’s what the Definitive Yii Guide has to say:

Would be cool to have other options, but it’s better than nothing. ;)

There is a patch at Google code:

Thanks for posting that patch, jacmoe. I haven’t tried it yet, but it looks promising. That’s one of those things that would be nice to see in the core.

I’ve just created an extension to route url’s based on requested method

Its here if interests you

Gustavo, jacmoe - it seems you interested in Yii+REST. I searched throw forum posts and found that most threads only mention url management (including Gustavo’s extension and recently fixed issue 802).

But as I understand when we receive PUT or DELETE request we have no access to the request parameters using standard methods. PHP does not populates these requests into superglobals as $_GET and $_POST and Yii’s CHttpRequest also deals with GET and POST only.

Am I wrong? Maybe I missing something, but it seems like for most people first part (url management) is enough. How the second part (get request parameters) is usually solved?

I now have CHttpRequest subclass and use file_get_contents(‘php://input’) to get PUT/DELETE parameters and this is discussed only here.

So maybe there is a simpler method to do this?

You are right

I actually extended the CHttpRequest to fit my needs, pretty much doing what the guy says in the link you posted

Yes, I am extending CHttpRequest too and use input parsing.

There is one more thing in the link I posted above - Qiang promised to add support for REST to the Yii and now support is added to the url manager, but not to the CHttpRequest.

So I think we should add one more ticket and ask for this. What do you think?

Yeah, it is a good idea

Added - issue 2024

Do not use SOAP if you can avoid it. It is awful. Do not use XML if you can avoid it. It is awful.

I am interested in finding tutorials, source code or video on how to create your own RESTful API (Server or Client) using YII.

I want to build a REST API (Server) that delivers JSON for a mobile iPhone application, but I really do not know where to start; or if YII will help me or how.

Has anyone built an API using YII? Where do I start?