Yii Framework Forum: use REST API as model source - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

use REST API as model source

#1 User is offline   Borner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 08-August 16

Posted 11 August 2016 - 03:48 AM

Hi,

in my case, my Yii2 application should not provide an REST API, but it have to act as an REST API client in order to read an write data to an REST service on an different host.

So my question: what is the best practice, to put the code for loading data from an REST Web Service? Is the controller the right place?
Or isn't the model the place for dealing with data?

Is it possible to connect an model with an remote REST API as data source (out of the box) instead of linking an model to an database (which works out of the box in yii)?

How would you handle this needs?

Thank you.
Borner
0

#2 Guest_MyersPi_*

  • Group: Guests

Posted 11 September 2016 - 01:51 PM

Bullhorn does not provide a Sandbox Account for REST API. Youll need to use a clients account.
0

#3 User is offline   mikezimin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 28-October 16

Posted 04 November 2016 - 01:07 PM

View PostBorner, on 11 August 2016 - 03:48 AM, said:

Is the controller the right place?


No.

View PostBorner, on 11 August 2016 - 03:48 AM, said:

Is it possible to connect an model with an remote REST API as data source (out of the box) instead of linking an model to an database (which works out of the box in yii)?


You can extend Model (not ActiveRecord class):
http://www.yiiframew...base-model.html
and use rules for validation.

But Model class has no find() and save() methods. You should code them (or other required) methods.
0

#4 User is offline   Borner 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 08-August 16

Posted 13 December 2016 - 10:23 AM

Thank you.

If I have my own model, which takes the data from an rest api.

Is there a way to create an AR object from that model? Because there are some interesting Yii features I like to use, but those features need an AR.
0

#5 User is offline   mikezimin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 28-October 16

Posted 14 December 2016 - 05:37 PM

View PostBorner, on 13 December 2016 - 10:23 AM, said:

Thank you.

If I have my own model, which takes the data from an rest api.

Is there a way to create an AR object from that model? Because there are some interesting Yii features I like to use, but those features need an AR.


What exactly features of ActiveRecord do you want to use?
0

#6 User is offline   itma 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 113
  • Joined: 16-September 13

Posted 27 December 2016 - 12:58 PM

I have been thinking about the question for a quite time... I have got a general idea on that subject. It could be done like it has been done for MongoDB which keeps the data as JSON structure. Maybe it is possible to get some ways to operate on that structures out and build something to cover the basic methods to operate on JSON. Or, finding a library written in PHP to operate on JSON which is already done & well tested and cover it within AR interface.

Or write just a wrapper for http://www.yiiframew...rrayhelper.html :-)

Just general idea to keep the subject up.
A full stack developer for hire (andrew@itma.pl)
https://twitter.com/Andrew_Bernat
0

#7 User is offline   itma 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 113
  • Joined: 16-September 13

Posted 11 April 2017 - 01:17 PM

Hey, I was digging through the github repos and have found https://github.com/yii2tech/filedb. I think you could take a closer look at this because it may be the thing (or very close to it) what you are looking for!
A full stack developer for hire (andrew@itma.pl)
https://twitter.com/Andrew_Bernat
0

#8 User is offline   skworden 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 491
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 18 May 2017 - 10:07 PM

your model would just extend model not activerecord. You can do everything that you can do in AR except the db query stuff which isn't needed since you're using an external api.

Your searchModel would use arraydataprovider instead of activedataprovider. The allModels would be the result of the api call. you can do everything you normally could if the data was coming from yii.

The save(), find(), etc. are not needed as api's use get, put, post, delete etc as the methods. You can define these in your controller and use the HttpClient module that Yii has to send and receive this data.

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
1 members, 1 guests, 0 anonymous users

  1. sonik_the_sonik