How to set up CakePHP 2.x to behave like a RESTful webservices (for using it together with JavascriptMVC)

StackOverflow https://stackoverflow.com/questions/11943294

Domanda

I am trying to set up cakephp to work with the very nice javascriptMVC (http://forum.javascriptmvc.com). JavaScriptMVC requires the JSON-Output in the following format:

[{
   'id': 1,
   'name' : 'Justin Meyer',
   'birthday': '1982-10-20'
},
{
   'id': 2,
   'name' : 'Brian Moschel',
   'birthday': '1983-11-10'
}]

Cake would generate a deeper nested array with a prepended Class Name. I found attempts to solve the problem but theyre not for cakephp 2.x. I know that I can simply generate a new array and json_encode() it via php, but it would be nicer to include a function like this https://gist.github.com/1874366 and another one to deflatten it. Where would be the best place to put such functions? The AppController doesnt seem to work. Should i put it in beforeRender () or beforeFilter() of the controller? Or does someone maybe even know of an existing solution/plugin for this? This would be the best for me in my current Situation, as Im pretty much pressed for time.

È stato utile?

Soluzione

Ok, I'm not 100% sure I understand what you are trying to do so here's a word to the wise just in case: Cake and JMVC are both comprehensive MVC frameworks. if you are attempting to combine them as a single cohesive platform to build your application, I strongly suggest you review your approach / platform / etc.

Also -- I'm not an expert by any means in jmvc, so I'm just going to pretend that processing the response from Cake in jmvc is completely out of the question, for some odd reason. For the record, think of Cake's responses like this:

{ "Model" : 
    [{
    'id': 1,
    'name' : 'Justin Meyer',
    'birthday': '1982-10-20'
    },
    {
    'id': 2,
    'name' : 'Brian Moschel',
    'birthday': '1983-11-10'
    }]
 }

Cake has had comprehensive REST service support, since at least Cake 1.2. The lib you are interested in is HttpSocket. As for json encoding and serving response, Request Handling covers, among other things, responding to all manners of requests, content types, decoding and encoding json, etc. Finally, the built-in Set utility will almost certainly cover whatever array manipulation you need in a line or two.

The functionality you are interested in is pretty basic and hasn't changed too much. I'd bet a lot of the (reasonably simple) solutions you have already found would probably still work, maybe with a little bit of tweaking.

For pretty much any basic service endpoint, you would probably create a controller (not AppController - that is application-wide, hence you can't invoke it directly) method, considering Cake routes the controller/action into your url:

Cake consuming services from a different app would look like this:

http://cakeproject/collect/getInfo

class CollectController extends AppController {

public function getInfo($array = null) {
    App::uses('HttpSocket', 'Network/Http'); 
    $http = new HttpSocket();
    $http->get('http://jmvcproject/controller/action', $array);
    // ...etc. 
} 

Cake providing services from the same controller / action to a different app would simply be:

public function getInfo($array = null) {
    $results = $this->Collect->find('all', $array);
    // ...fetch the results
} 

Or you could just loop over that array with foreach($this->data as $data) { ... to drop the class name. But if your data will include associated models, etc, Set is probably the most versatile and resilient solution.

Anyway, HTH

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top