hiqdev / yii2-hiart
ActiveRecord for API
Installs: 44 836
Dependents: 5
Suggesters: 1
Security: 0
Stars: 58
Watchers: 15
Forks: 14
Open Issues: 3
Type:yii2-extension
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
- hiqdev/hidev-hiqdev: <2.0
- hiqdev/hidev-php: <2.0
- hiqdev/yii2-hiart-github: dev-master
- yiisoft/yii2: dev-master
README
ActiveRecord for API
This Yii2 extension provides ActiveRecord implementation that allows to access remote/web API same way as you do with normal Yii2 DB ActiveRecord.
Installation
The preferred way to install this yii2-extension is through composer.
composer require "hiqdev/yii2-hiart"
But if you prefer Guzzle transport use yii2-hiart-guzzle
package:
composer require "hiqdev/yii2-hiart-guzzle"
Configuration
To use this extension, configure hiart component in your application config:
'components' => [ 'hiart' => [ 'class' => \hiqdev\hiart\rest\Connection::class, 'requestClass' => \hiqdev\hiart\auto\Request::class, 'baseUri' => 'https://site.com/api/v3/', ], ],
Note three main options:
class
specifies class that actually implements API to be accessed, REST in this caserequestClass
specifies transport implementation to be used, auto in this casebaseUri
specifies starting point of the API
Transports
Available transports are:
- auto - autodetects best supported transport
- PHP streams, the most generic fallback, included in this package
- cURL, included in this package
- Guzzle, provided with yii2-hiart-guzzle
- yii2-httpclient, provided with yii2-hiart-httpclient
You can implement your own transport.
All you need is to create two classes: Request
and Response
, it's not difficult see available implementations.
Transport can be even non-HTTP based.
Query builders
QueryBuilder is what actually implements an API.
We are developing QueryBuilder
s for:
- Basic REST API, included in this package
- GitHub API, provided with yii2-hiart-github
- HiPanel API, provided with hipanel-hiart
You can implement your own API.
Basically all you need is create your QueryBuilder
class with these methods:
buildMethod(Query $query)
buildHeaders(Query $query)
buildUri(Query $query)
buildQueryParams(Query $query)
buildBody(Query $query)
buildFormParams(Query $query)
See available implementations and ask questions using issues on GitHub.
Usage
Define your Model:
class User extends \hiqdev\hiart\ActiveRecord { public function rules() { return [ ['id', 'integer', 'min' => 1], ['login', 'string', 'min' => 2, 'max' => 32], ]; } }
Note that you use general hiqdev\hiart\ActiveRecord
class not specific for certain API.
API is specified in connection options and you don't need to change model classes when
you change API.
Then you just use your models same way as DB ActiveRecord models.
$user = new User(); $user->login = 'sol'; $user->save(); $admins = User::find()->where(['type' => User::ADMIN_TYPE])->all();
Basically all the features of Yii2 ActiveRecord work if your API provides them.
License
This project is released under the terms of the BSD-3-Clause license. Read more here.
Copyright © 2015-2018, HiQDev (http://hiqdev.com/)
Acknowledgments
- This project was started as fork from Yii2 Elasticsearch.