Thoughts

Recent Posts

CodeIgniter "2.0": Rumours, facts and requests
Ever since it was announced that ExpressionEngine 2.0 would be running on CodeIgniter, the forums have been jammed full of "CodeIgniter 2.0" questions. I have answer this so many times I wanted to clear things up and put some useful suggestions forward.

Speaking at EECI2009 in Leiden, Holland
As a massive fan of CodeIgniter I was pretty excited to be attending the EECI2009 conference in Leiden, Holland, as it would give me the chance to meet the EllisLabs development team and lots of other CodeIgniter nerds. Now as a last minute replacement I will actually be speaking at the event too!

Asset handling in CodeIgniter with the BASE tag
There are many suggested ways to handle your assets in CodeIgniter (including my very own Asset library) but this solution has to be one of the easiest. Read on to find out what it is!

Clearing CodeIgniter session data
This article shows a few ways of clearing session data in CodeIgniter, inlcuding a non-documented approach which lets you wipe all session data without destroying the session.

UTF-8 support for CodeIgniter
Writing a CodeIgniter application is easy. Writing a CodeIgniter application that supports all characters from multiple languages? Not so easy. Find out how to support UTF-8 content in CodeIgniter.

REST implementation for CodeIgniter

Posted: Jun 03, 2009
Category: CodeIgniter


I have seen 1 or 2 RESTful implementations for CodeIgniter but the syntax and methodology for each of them left me feeling like it could be done better.

Firstly, REST is not something that can be put into a library. It is not a "thing" and cannot be treated as such.

Secondly, REST calls should not be mixed in with your normal controllers. It should be kept separate from your normal controllers as they are not the same. The default behavior should be slightly different as REST calls are not simple GET -> output.

Thirdly, REST controllers need to remain flexible. For that reason the normal CodeIgniter URI segments are not enough. To keep the controllers flexible, associative URI segments should be enforced so any combination of parameters can be used.

Finally, REST controllers should be able to output the response in many number of formats. These formats should be specified by the requester and not set in stone within the code. This means a REST controller needs to detect the desired format in two different ways.

  • via the $_SERVER['HTTP_ACCEPT'] variable. This will allow cURL, telnet, etc to specify a method through standard HTTP headers. This is the recommended REST way.
  • via the URL. This allows browsers and systems not using cURL, telnet, etc to make very basic requests to receive different formats.
  • via the controller. Adding "public $rest_format = 'json';" will make that one controller default to json if nothing is provided by given via URL or HTTP_ACCEPT.

It took a great deal more thought and consideration than it did actual implementation. Here is the code I have used to get my CodeIgniter RESTful implementation working, tested and complete.

application/libraries/REST_Controller.php - This new controller type will contain all of the logic for our new REST controllers.

application/config/rest.php - Control login restrictions and caching for your REST server.

application/controllers/example_api.php - This is an example of a REST controller with some basic user data. This can be called anything you like and be placed in Matchbox modules, sub-directories, whatever.

Useage

Now it is all set up and ready go. Crack open your browser and try URL's in these formats to see what happens:

Get one users profile

http://localhost/codeigniter/index.php/example_api/user/id/435

Get a list of users in HTML format

http://localhost/codeigniter/index.php/example_api/users/users/format/html

Try to access an object through an unsupported method

http://localhost/codeigniter/index.php/example_api/user_put/id/435/name/Something

Personally, I would recommend using Matchbox to get your controllers into modules and then put a api.php controller into each module you wish to send over the API.

E.g http://localhost/codeigniter/index.php/news/api/article/id/465

As you can see there is plenty of flexibility here. You can use any of the following formats too simply by appending /format/json to the end of your URL or passing the correct MIME-type:

  • xml
  • json
  • serialize
  • php (raw output that can be used in eval)
  • html
  • csv

Within your REST controllers, remember you can access GET parameters through $this->get(), POST parameters through $this->post() and PUT parameters through $this->put().

Let me know what you think of this in the comments, tweet me or use the contact form.



Comments

They said...

Posted: Oct 21, 2009
From: Muser

Sorry the last message I posted haven been trimmed.

Forget it. Now It works ok when I've upgraded my php version (I was using php 5.1.4). Now with php 5.2.11 works very well.


Posted: Oct 21, 2009
From: Josh Giese

wow, this is great. The community surrounding CodeIgniter is another reason this framework rocks. Thank you for this contribution. I'm working on an iPhone app, and I need to build out the services this connects to. Your code is going to be the foundation for that. Thanks again.


Posted: Oct 18, 2009
From: Muser

Hi I get the following xml when I query users:

<xml>



</xml>


Posted: Sep 02, 2009
From: Phil Sturgeon

Mario: Yes query strings are available via $this->get() in the ame way as the URI segments.

Dan S: Tweet me and i'll follow you. Sadly thats the way you can DM me.


Posted: Aug 01, 2009
From: Dan S

Phil, thanks for this... just started following you on Twitter but really can“t work out how to send a direct message!

Was wondering... the rest.php seems to point to rest_controller.php on this page

Sorry if it is a stupid question... im not particularly experienced with codeigniter but trying to get a REST interface up to develop some ning apps.

Cheers, Dan


Posted: Jun 23, 2009
From: Mario

Can query strings be used too, such as
?q1=...&q2;=...


Posted: Jun 19, 2009
From: Phil Sturgeon

I'm glad this has helped you Jared. I have e-mailed you a few details but just to let other readers know; this has been updated with a new version of the Rest Server which can be found on GitHub (links in the article).

This new version supports HTTP Basic & Digest Authentication via the config/rest.php file.


Posted: Jun 12, 2009
From: Jared

hey, i just wanted to say thanks for developing rest for CI. I am just starting a web services implementation on a project i'm working on and your timing couldn't be better.
I wanted to let you know as I've been working with your files, there were a couple of small items I changed to get everything working on my end.

1. I added all of the api files in a subdirectory of my controllers. In doing so, I had to change $this->uri->uri_to_assoc() to $this->uri->ruri_to_assoc() to properly map the uri associations. this will also apply to any other type of routing changes someone might apply.

2. The method var_export in function _php() should have the second param set to true to return the value instead of dumping it in the function.

Other than those two items everything appears to be working flawlessly! I also implemented the basic auth you're working on in the forums.


You say...?