simialbi/yii2-rest-client

REST client (AR-like model) for Yii Framework 2.0 (via yii2-http-client, extends ApexWire/yii2-restclient)

Installs: 16 309

Dependents: 1

Suggesters: 0

Security: 0

Stars: 20

Watchers: 4

Forks: 10

Open Issues: 1

Type:yii2-extension

2.0.0 2022-07-28 07:23 UTC

README

This extension provides an interface to work with RESTful API via ActiveRecord-like model in Yii 2. It is based on ApexWire's yii2-restclient.

Latest Stable Version Total Downloads License Build Status

Resources

Installation

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require --prefer-dist simialbi/yii2-rest-client

or add

"simialbi/yii2-rest-client": "*"

to the require section of your composer.json.

Configuration

To use this extension, configure restclient component in your application config:

    'components' => [
        'rest' => [
            'class' => 'simialbi\yii2\rest\Connection',
            'baseUrl' => 'https://api.site.com/',
            // 'auth' => function (simialbi\yii2\rest\Connection $db) {
            //      return 'Bearer: <mytoken>';
            // },
            // 'auth' => 'Bearer: <mytoken>',
            // 'usePluralisation' => false,
            // 'useFilterKeyword' => false,
            // 'enableExceptions' => true,
            // 'itemsProperty' => 'items'
        ],
    ],

Usage

Define your Model

<?php

namespace app\models;

use simialbi\yii2\rest\ActiveRecord;

/**
 * MyModel
 * 
 * @property integer $id
 * @property string $name
 * @property string $description 
 * 
 * @property-read MyOtherModel $myOtherModel
 */
class MyModel extends ActiveRecord {
    /**
     * {@inheritdoc}
     */
    public static function modelName() {
        return 'my-super-model-name';
    }

    /**
     * {@inheritdoc}
     */
    public static function primaryKey() {
        return ['id'];
    }
}

/**
 * Class MyOtherModel
 * 
 * @property integer $id
 * @property integer $my_model_id
 * @property string $subject
 * 
 * @property-read MyModel[] $myModels
 */
class MyOtherModel extends ActiveRecord {
    /**
     * {@inheritdoc}
     */
    public static function primaryKey() {
        return ['id'];
    }
}

It's important that you define the primary key by overriding primaryKey() method. Otherwise you'll get an exception. If you do not override the modelName() method, it will guess it by class name (MyModel becomes my-model). It's used to generate the URL together with simialbi\yii2\rest\Connection::$baseUrl.

The usage how to define the active record (rules, behaviors etc.) is the same like yii\db\ActiveRecord.

Important: Be sure to either define the properties of the object like in the example above (@property syntax in phpdoc) or override the attributes() method to return the allowed attributes as array

The same about relations. Be sure to either define them via @property-read phpdoc comment or override the getRelations method. If the related class has not the same namespace as the main class, be sure to use the fully qualified class name (e.g. @property-read \app\models\OtherModel[] $otherModels)

License

yii2-rest-client is released under MIT license. See bundled LICENSE for details.

Acknowledgments