Jacob Clark
Jacob Clark

Reputation: 3447

Model from URL/API/JSON CakePHP

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.

Upvotes: 2

Views: 1360

Answers (2)

Dave
Dave

Reputation: 29121

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.

Upvotes: 0

AD7six
AD7six

Reputation: 66168

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

Upvotes: 4

Related Questions