How can I create a PHP Kohana application with restful webservices?

305 Views Asked by At

I am very very new to PHP and Kohana. Already created a sample/demo "hello World" PHP Kohana application which is running sucessfully in WAMP server.

I want my application to be work as a complete server side component.

Since i'll have only server side logic in this application, it should use a ORM to communicate with my MySQL database.

I'll have a separate client side application which will have UI parts.

So I want my PHP-Kohana should recognize the RestFul webservices call from my client and give the JSON response accordingly.

Is it possible to create a Kohana application which supports RestFul webservices?

If yes, give me a guidance to create the webservices in Kohana application.

Is there any such sample code for demo?

1

There are 1 best solutions below

0
SigmaSteve On

There is no specific demo or sample code that I know of, so hopefully these tips will help to get you started with it...

It is possible, and relatively easy, to accept AJAX requests and produce JSON responses with Kohana. The first thing to be aware of is that unless told otherwise, Kohana will always try to generate the view, and this will fail as a JSON response so first things first:

if ($this->request->is_ajax()) {
    // Disable any rendering of the template so it just returns json.
    $this->auto_render = FALSE;
}

You'll probably want to put this in the before() method, probably of the parent Controller so that it always runs before you are getting data from the DB.

My personal preference for something like this would be to set up a standard AJAX response array so that the data is always returned in a relatively standard format. Example:

// Standard ajax response array.
$this->ajax_response = array(
    'success' => FALSE,
    'error' => NULL,
    'raw_data' => NULL,
    'generated' => ''
);

Customise the above to match your required usage. You'll probably also want this in your before() method.

Now in your action methods, get the data from the DB and add it to the array.

public function action_foobar() {
    // Get the primary key ID from the URL.
    $var1 = $this->request->param('var1');

    $data = ORM::factory('Model', $var1);
    if ($data->loaded()) {
        $this->ajax_response['success'] = TRUE;
        $this->ajax_response['raw_data'] = $data;
    } else {
        $this->ajax_response['error'] = 'Data could not be found.';
    }
}

You should then be able to request this data by calling a URL such as http://www.website.com/controller/foobar/42

The final piece of the puzzle is actually returning this data, as at the moment Kohana won't output anything because we have told it not to. In your after() method, do the following:

if ($this->request->is_ajax()) {
    $this->request->headers('Content-Type', 'application/json');
    $this->response->body(json_encode($this->ajax_response));
}

Then you're free to interpret that response however you see fit in the jQuery on your client-side application:

$.ajax({
    type: "POST",
    url: "http://www.website.com/controller/foobar/" + foobarId,
    dataType: 'json',
    success: function (data) {
        if (!data.success) {
            alert(data.error);
        } else {
            // Handle the returned data.
        }
    },
    error: function (xhr, status, errorThrown) {
        // Something went very wrong (response failed completely).
        alert(errorThrown);
    }
});

Good luck with building your app! I hope this helps to at least get you started.