Introducing...REST! - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Introducing...REST! (/showthread.php?tid=6519) |
Introducing...REST! - El Forum - 03-02-2008 [eluser]Lovecannon[/eluser] Hmm, if your anything like me, the fact that CI only supports XML-RPC is not enough. And unless you've been in a hole the last few years..then you've probably heard of REST, which stands for Representational State Transfer, which is a simple way of providing web services, using HTTP as a transport, and using URLs to identify services. I personally, am not a big fan of XML, so I'm not too big on protocols like XML-RPC and SOAP(mostly SOAP, because SOAP is such a hassle to screw with), and so, I looked around the CI forums for a REST library, and apparently there isn't one. So, I wanted to make an easy way to implement REST in CodeIgniter(and also just PHP), which is just as simple as CI's XML-RPC interface. So thus, the Lovable REST library was born. Now onwards, to API docs. First, copy Rest.php into application/libraries. To use it, your going to have to load it, example: Code: $this->load->library('rest'); After it is loaded, the library will be available at $this->rest, and the methods are as follows: You can add methods via the addFunction function, example: Code: $this->rest->addFunction('name of service method to be used as a url identifier', 'name_of_method', 'data type'); /*Btw, the name of the method is a method of whatever object is passed via the serve function. The data type parameters can be: get, post, raw, and none. In order to use get as a service, you have to set your URI protocol to PATH_INFO or ORIG_PATH_INFO in your config.php. If you want to use PUT and DELETE, use raw as your data type.*/ Other functions in the API are jsonEncode, jsonDecode(which are just wrappers to decode/encode JSON), and serve, which propagates the request (You have to pass a reference of the object in which the API methods will reside, if they are in your controller, your $this), getRawInput, which returns the raw http post data, and enableGet, which you can use to enable $_GET manually. Now to put it all together, here is an example of all of it in action: Code: class Api extends Controller so say, if the above controller was located at http://ilikecheese.com, you could access the testpost service at http://ilikecheese.com/api/rest/function/testpost So...sorry for the long-winded rant, if anyone gets confused by my attempt at API docs, please post your question, and I will clarify. Download: http://www.mediafire.com/?t4g753ydmtp Introducing...REST! - El Forum - 03-09-2008 [eluser]ezafy[/eluser] thank you so much for sharing it )))))))))) Introducing...REST! - El Forum - 03-09-2008 [eluser]Lovecannon[/eluser] No problem. Right now, its pretty basic, but I will try to develop some more functions, and I hope you find it useful! Introducing...REST! - El Forum - 03-11-2008 [eluser]Tony Nash[/eluser] Excellent! Keep up your good work , I just can't wait to see the next version Introducing...REST! - El Forum - 04-04-2008 [eluser]ecarsted[/eluser] Hi, I am getting A PHP Error was encountered Severity: Notice Message: Undefined variable: array Filename: libraries/Rest.php Line Number: 25 I am sure that it has to do with the fact that I am using PHP 5.2 and Apache 2.2.6 $_SERVER['PATH_INFO'] = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : ''; The variables are not set. Not sure what I should be setting this to. Eric Introducing...REST! - El Forum - 04-04-2008 [eluser]Lovecannon[/eluser] Your problem is you do not have your URI protocol set to PATH_INFO, in your config/config.cfg, change $config['uri_protocol'] to "PATH_INFO" Introducing...REST! - El Forum - 04-05-2008 [eluser]ecarsted[/eluser] Fixed it, just commented out // $_SERVER['PATH_INFO'] = (isset($_SERVER['ORIG_PATH_INFO'])) ? $_SERVER['ORIG_PATH_INFO'] : ''; Eric Introducing...REST! - El Forum - 04-05-2008 [eluser]James Pax[/eluser] nice Introducing...REST! - El Forum - 04-05-2008 [eluser]Lovecannon[/eluser] Hmm...Im gonna have to fix that..but Im going to have an update of my REST library soon, that will try and fix that error, and add some extra features. Introducing...REST! - El Forum - 04-05-2008 [eluser]garymardell[/eluser] mmm I am not sure how i would go about using this to provide an API. Any chance someone could assemble a sample controller showing a request being made to get an array of values and return as json? Would be much appreciated. |