yii2-hiart

ActiveRecord for API

  • 所有者: hiqdev/yii2-hiart
  • 平台:
  • 許可證: BSD 3-Clause "New" or "Revised" License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

HiArt

ActiveRecord for API

Latest Stable Version
Total Downloads
Build Status
Scrutinizer Code Coverage
Scrutinizer Code Quality
Dependency Status

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 case
  • requestClass specifies transport implementation to be used, auto in this case
  • baseUri specifies starting point of the API

Transports

Available transports are:

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 QueryBuilders for:

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

主要指標

概覽
名稱與所有者hiqdev/yii2-hiart
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證BSD 3-Clause "New" or "Revised" License
所有者活动
創建於2015-05-24 15:56:47
推送於2025-07-03 12:52:28
最后一次提交2025-07-03 15:52:10
發布數10
最新版本名稱0.4.0 (發布於 )
第一版名稱0.0.1 (發布於 )
用户参与
星數59
關注者數14
派生數15
提交數380
已啟用問題?
問題數15
打開的問題數2
拉請求數15
打開的拉請求數1
關閉的拉請求數6
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?