Model from URL/API/JSON CakePHP

1.3k Views Asked by At

I have a new CakePHP install and I have a series of Web Service Endpoints in JSON, is it possible to populate a Model from this JSON in CakePHP?

They won't be connecting to a DB as all data comes from and submits to the web service.

I have no code as I cannot find any documentation on the CakePHP Models Site.

2

There are 2 best solutions below

3
On

Rest datasource

The storage mechanism that a model uses is a datasource. By default Cake assumes a model will be used with a database but it can be anything that extends the Datasource class. There's an example in the documentation of a simple REST api datasource, and there are many more datasources in the community datasources repository.

It's not necessary to build your own REST datasource though, there are existing solutions out there such as this one which make using a model with a REST api rather trivial.

Examples from the readme:

User::find('all')               == GET    http://api.example.com/users.json
User::read(null, $id)           == GET    http://api.example.com/users/$id.json
User::save()                    == POST   http://api.example.com/users.json
User::save(array('id' => $id))  == PUT    http://api.example.com/users/$id.json
User::delete($id)               == DELETE http://api.example.com/users/$id.json
0
On

I asked the same question and was told to look into this: https://github.com/UseMuffin/Webservice

"Bringing the power of the CakePHP ORM to your favourite webservices."

I have yet to try it, but it seems like exactly like what we want.