Friday, June 22, 2012

Re: How to do a REST WS?

Hi, Lucas.

Firstly if you're developing a REST service it's best to use Cake 2.x and higher - you can just use the latest stable version. The new Cake version has a lot of improvements which will come in handy when you're doing a REST service.Then how would you go about building your REST sevice? I will try to describe this as
a general overview of how things should all work together:

Firstly you should set up some of the aspects of Routing.
  Concerning Router::mapResources(); is the fast way to go - it "is used to setup a number of default routes for REST access to your controllers".
If you want a more fine grade setup you should consider using custom REST routing which is what I personally prefer using:

    Router::connect('/:candidates/addrecord', array('controller'=> 'candidates', 'action' => 'addRecord', '[method]' => 'POST'));
    Router::connect('/:candidates/editrecord', array('controller'=> 'candidates', 'action' => 'editRecord', '[method]' => 'POST'));
    Router::connect('/:candidates/deleterecord', array('controller'=> 'candidates', 'action' => 'deleteRecord', '[method]' => 'POST'));

  Once you've configured the routes you can proceed to identifying requests. Nevertheless this is still part of the initial Router configuration:
   Router::parseExtensions('xml','json','rss'); - This will instruct the router to parse out file extensions from the URL for e.g.:
http://www.example.com/articles.xml would parse a file extension of "xml". What this will yield is that the parsed file extension will become available in the Controller's
$params property (in $this->params['ext']). This property is used (runtime) by the RequestHandler component to automatically switch to alternate layouts and templates, and load helpers corresponding to the given content. So this will greatly help you in the development of a REST service, if you set all your layouts/views by the conventions.

So what about handling and serving responses to requests? Firstly you should add the RequestHandler component. If you will be using it in all controllers (which should be the case :) ) you should add it the AppController's $components property:

    public $components = array(
            'DebugKit.Toolbar',
            'Session',
            'Auth' => array(
                'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
            ),
            'RequestHandler'
    );

When the application receives a request for e.g. on: http://www.example.com/records.xml by default this will call RecordsController::index().
Here's an example structure of a add method:

public function addRecord() {
    if ($this->request->is('post')) {
        //Authentication ?
        //Validate incoming data
        if ($this->RecorisXmld->saveAll($data)){
            if($this->RequestHandler->isXml()){
                //Serve a Xml responce
            }
            if($this->RequestHandler->isRss()){
                //Serve RSS
            }
        }
    }
}


CakePHP now (since 2.1) has Json and Xml view classes. What this will do is that for e.g. "After adding 'json' to Router::parseExtensions() in your routes file, CakePHP will automatically switch view classes when a request is done with the .json extension, or the Accept header is application/json."

So this basically is it. By enabling Cake's core features you could be able to easily manage a REST service. Hope this helped.

Cheers,
   Borislav.

On Thursday, 21 June 2012 20:26:28 UTC+3, Lucas Simon Rodrigues Magalhaes wrote:
Hello I need to build an application in Webservice to return data for a particular billing.

First I'm using cakePHP version 1.3, and following the cake book [1].
According to according to the book I should map the model which give permission to access REST.
I thought about using REST to provide data json / xml instead of nusoap, but I'm sure how to do it.

First I'm using cakePHP version 1.3, and following the cake book [1].
According to according to the book I should map the model which give permission to access REST.

The problem:
In this case I need to perform a find in various tables and perform an operation with her results in a foreach, and then send the answer to this operation via JSON / XML.

What I did:
I created a controller called ws_billing_controller.php that will manage the requests get to the rest.
routes.php -> Router :: mapResources ('') / / empty do not know what to put here
routes.php -> Router :: parseExtensions ('json', 'xml');
ws_billing_controller.php -> [2]

I wonder if the path I'm following this right, or should I change?

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate