Welcome Guest, Not a member yet? Register   Sign In
Rest Integration
#1

[eluser]Kindari[/eluser]
I've been hearing a lot about REST lately, even dabbled a little bit with it when I tried RoR, but I've never really used it.

I understand the ideas behind it and the general idea of how things work. My question is how easy is it to get going? I know of Phil Sturgeons REST implementation, and I figured if I were to go the REST route I would want all my controllers to be RESTful and default to html output. Is that sort of thing easy to do?

Is there anything else I should consider before implementing it?
#2

[eluser]Cristian Gilè[/eluser]
Have you already read this?
#3

[eluser]Kindari[/eluser]
I've read that but I guess I didn't quite convey what I was after above..

I Basicly want to make Rest_Controller my MY_Controller. But I want to override its functionality.

Say I have my User controller which extends Rest controller. If no format is specified, I want it to actualyl show my current website. but if a format is specified, REST renders it as normal.

Maybe this should be done as something like..
Code:
if ($format) {
    $this->response($data)
}
else
{
    $this->load->view('user');
}

Does this make sense? is it feasable?
#4

[eluser]Nick_MyShuitings[/eluser]
I do something similar in all my controllers that that if you access it via the normal url you get the whole layout with various views etc, but if it is ajax you only get the data for the main content view in json. I suppose you could add an additional cause that would return the data in a different format if the request is rest.

Interesting... I bet it would be a lot of work. What's the main advantage that you see? IE. what would all of that extra work provide you with?
#5

[eluser]InsiteFX[/eluser]
You can download Phil's Rest Server here:

philsturgeon - codeigniter-restserver

InsiteFX
#6

[eluser]Cristian Gilè[/eluser]
Potecting your REST service with an API token could be a solution. If you match a valid API token in the URL handle the request as REST.

Cristian Gilè
#7

[eluser]Kindari[/eluser]
Its not that I want to protect the server - in fact I want it to be completely open.

What I want is to keep it DRY, the same controller shows a web page, and if its a REST request handles it restfully. I don't know though, if it is *possible* to do it this way. I'm not sure if this modifies something in the controller that would prevent a controller from working normally.

SO basically, I don't want the REST controller to be ran if a format is not specified. Instead I want it to show a normal looking website page.

To sum up as I collect my thoughts because I've been writing this post as people ask me about CRM magical rainbows; whereas the normal method would have been a "users" controller for the website and a "users_api" controller for REST, I want to know if it is feasible to combine the two.
#8

[eluser]Nick_MyShuitings[/eluser]
I really like the idea, and its definitely possible. I don't think it will be easy though. And you'd want a way to control on the off chance.

I run a modified version of Jamie Rumbelow's My_Controller, and the way I'd tackle this would be to hook into the _remap function.

Basically run a check to see if the request is rest or ajax, and in that case return the json array of the main data. Interesting idea. Keep us posted on if you make it work, and if I get around to it I'll make sure to PM you.
#9

[eluser]Kindari[/eluser]
I got this working, not sure if it's the "right" way to do it, but it works so Far.

I added a variable before the constructor:

Code:
protected $is_rest = TRUE;

Then in _detect_format, I added the variable check just before the end of the method where it falls back to default formats:
Code:
$this->is_rest = FALSE;
        if (!empty($this->rest_format))
        {
            return $this->rest_format;
        }

        // Just use the default format
        return config_item('rest_default_format');

Finally, My controller:

Code:
class Welcome extends MY_Controller { // MY_Controller is extending REST_Controller

    function __construct()
    {
        parent::__construct();
    }

    function index_get()
    {
        if ($this->is_rest) {
            $data = array('test' => array('a' => 'b'));
            $this->response($data);
        }
        else
        {
            $this->load->view('welcome_message');
        }
    }
}

I also really like this method because it is still extending the rest implementation, allowing me even if I don't finish the request "restfully" to separate the methods based on GET, POST, etc.




Theme © iAndrew 2016 - Forum software by © MyBB