2017 © Pedro Peláez
 

yii2-extension yii2-rest-client

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

image

simialbi/yii2-rest-client

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

  • Monday, October 16, 2017
  • by simialbi
  • Repository
  • 0 Watchers
  • 2 Stars
  • 201 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 15 % Grown

The README.md

REST Client for Yii 2 (ActiveRecord-like model)

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., (*1)

Latest Stable Version Total Downloads License Build Status, (*2)

Resources

Installation

The preferred way to install this extension is through composer., (*3)

Either run, (*4)

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

or add, (*5)

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

to the require section of your composer.json., (*6)

Configuration

To use this extension, configure restclient component in your application config:, (*7)

    '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'
        ],
    ],
Parameter Default Description
baseUrl '' The location of the api. E.g. for http://api.site.com/v1/users the baseUrl would be http://api.site.com/v1/ (required)
auth Either a Closure which returns a string or a string. The rest connection will be passed as parameter.
usePluralisation true Whether to use plural version for lists (index action) or not (e.g. http://api.site.com/users instead of user)
useFilterKeyword true Whether to use "filter" key word in url parameters when filtering (e.g. ?filter[name]=user instead of ?name=user
enableExceptions false Whether the connection should throw an exception if response is not 200 or not
itemsProperty If your items are wrapped inside a property (e.g. items), set it's name here
requestConfig [] Client request configuration
responseConfig [] Client response configuration
updateMethod 'put' The method to use for update operations.
isTestMode false Whether we are in test mode or not (prevent execution)
enableQueryCache false Whether to enable query caching
queryCacheDuration 3600 The default number of seconds that query results can remain valid in cache
queryCache 'cache' The cache object or the ID of the cache application component

Usage

Define your Model, (*8)

<?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., (*9)

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

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, (*11)

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, (*12)

License

yii2-rest-client is released under MIT license. See bundled LICENSE for details., (*13)

Acknowledgments

The Versions

16/10 2017

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

by Avatar ApexWire
by Avatar simialbi

rest yii2 model client active record

16/10 2017

1.0.3

1.0.3.0

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

  Sources   Download

MIT

The Requires

 

by Avatar ApexWire
by Avatar simialbi

rest yii2 model client active record

16/10 2017

1.0.2

1.0.2.0

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

  Sources   Download

MIT

The Requires

 

by Avatar ApexWire
by Avatar simialbi

rest yii2 model client active record

16/10 2017

1.0.1

1.0.1.0

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

  Sources   Download

MIT

The Requires

 

by Avatar ApexWire
by Avatar simialbi

rest yii2 model client active record

13/10 2017

1.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

by Avatar ApexWire
by Avatar simialbi

rest yii2 model client active record