1

One of my views have to display data that is from an external API call.

To manage all of these API calls i have create a component.

Now in order to costum paginate i have to create a function inside of my model however since i'm not using the models table but instead the component i have to call that component inside my model.

Is this possible or is there a work around?

Marc Rasmussen
  • 19,771
  • 79
  • 203
  • 364
  • Solution: http://stackoverflow.com/a/25404606/216084 (cannot comment if this is a best-practice). –  Sep 19 '14 at 06:59

2 Answers2

2

To manage all of these API calls i have create a component.

This is plain wrong, mostly because you don't have to. Technically you can but it would be wrong architecture wise.

The most correct way to implement API access in CakePHP would be to create a data source for that API and use that data source with a model.

The data source page in the CakePHP book comes with a complete example of how to create a data source that calls a foreign API.

Doing it as a data source allows you to use the API with multiple models which makes sense if the API provides different things. For example you could have a GithubUser and a GithubRepository model using a GithubDatasource. Also the data source is easier to test then a component.

floriank
  • 25,546
  • 9
  • 42
  • 66
  • so what your saying is that i should instead of having a component use a model? – Marc Rasmussen Aug 21 '13 at 12:42
  • Read my text again it is very clear what I'm saying: I'm saying use a data source and use it with a model. Data sources are the piece that talk to a DB or an API a model is the piece that does something with the data delivered by a data source. – floriank Aug 21 '13 at 13:03
  • 1
    "display data that is from an external API call" clearly means displaying data that come from another source. So @burzum 's answer and Cake structure are quite logical for doing this: create a new datasource. – nIcO Aug 22 '13 at 06:48
0

I will suggest better to implement all functionality of component into model by creating functions within model.

And you can share that model for multipal controller.

and as your problem, over here you can use your model function within model.

Er.KT
  • 2,852
  • 1
  • 36
  • 70