CodeIgniter Forums
REST implementation for CodeIgniter - 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: REST implementation for CodeIgniter (/showthread.php?tid=19299)



REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Phil Sturgeon[/eluser]
Finally got a fully working REST implementation with support for JSON, XML, CSV, HTML tables, PHP export and serialization.

It supports multiple POST, GET, PUT and DELETE methods within a single rest controller and provides the methods $this->get(), $this->put() and $this->post() for accessing parameters from within the controller that all run through the input library for XSS protection.

Check out my article REST implementation for CodeIgniter.

You can have one rest.php or api.php file for your ENTIRE site, or using modules/sub-directories you can have one for each section.

Quote:/blogs/api/article/id/21
/blog/api/articles/limit/10
/blog/api/comments/limit/5/format/html
/gallery/api/comments/limit/5/format/json

Let me know what you think. I'd love some feedback on this.


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Dam1an[/eluser]
Excellent work Phil (as always) Wink
Although it will be a while before I need to use this, it looks simple enough Smile


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Yorick Peterse[/eluser]
You stole my API ! Tongue


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Dam1an[/eluser]
[quote author="Yorick Peterse" date="1244061632"]You stole my API ! Tongue[/quote]

I think the term you're looking for is 'enhanced' Tongue
And just cause you started one first means no one else can happen to do one at the same time? Tongue


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Phil Sturgeon[/eluser]
I modified it at first but it has evolved past all recognition. I didn't simply add a few more functions into your work, I re-coded the whole logic too it.

The only code I copied in the end was:

Code:
$_SERVER['HTTP_ACCEPT']

but even that changed to:

Code:
$this->input->server('HTTP_ACCEPT')

I have to say your API was a great source of inspiration though, helped out a lot. :-)


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]Yorick Peterse[/eluser]
Haha, I don't mind Tongue I have given myself a month to finish the API for Flork, so it will improve a lot

p.s. Phil, your logo is ready Smile


REST implementation for CodeIgniter - El Forum - 06-03-2009

[eluser]trice22[/eluser]
Great—just what I needed. I'll definitely check it out.

And in case it helps: I'm using a small extension for the Router to fetch the response format:
Code:
class MY_Router extends CI_Router
{
    public function _validate_request($segments)
    {
        /* get response format */
        $last_segment = $segments[count($segments)-1];
        $last_segment_arr = split('\.', $last_segment);
        $segments[count($segments)-1] = $last_segment_arr[0];

        if ( isset ($last_segment_arr[1]))
        {
            $this->response_format = $last_segment_arr[1];
        }
        
        [...]

    }
}

Now I can use URLs like
Code:
http://www.example.com/controller/method.json
and get the response format via
Code:
$this->router->response_format;
in my controller.

It's not pretty but it seems to work fine so far. I'm happy about better suggestions, though.

Cheers,
—trice


REST implementation for CodeIgniter - El Forum - 06-05-2009

[eluser]Phil Sturgeon[/eluser]
That is a possibility but I felt dirty enough adding /format/xml onto it. People should be requesting formats properly using MIME types instead of worrying about pretty URL's.

I'm amazed with the traffic coming in for this article. In two days it has almost caught up with the article introducing PyroCMS which was published a month ago!