Rest classes for using in your api module.

Installs: 2 227

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 2

Open Issues: 3


dev-master 2016-07-08 09:05 UTC

This package is not auto-updated.

Last update: 2020-10-02 21:10:02 UTC


Rest classes for using in your api module. For individual use!


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist platx/yii2-rest "*"

or add

"platx/yii2-rest": "*"

to the require section of your composer.json file.


In your model you can set fields and extraFields for using in API:

class MyModel extends \yii\db\ActiveRecord
    public function fields()
        $items = parent::fields();
        return $items;
    public function extraFields()
        return [
            'custom_field' => 'customField'
    public function getCustomField()
        return 'Custom value';

Create search form class for your ActiveRecord model:

class MySearchForm extends \platx\rest\SearchForm 
    public $modelClass = '\common\models\MyModel';
    public function rules()
        return \yii\helpers\ArrayHelper::merge(parent::rules(), [
    public function attributeLabels()
        return \yii\helpers\ArrayHelper::merge(parent::attributeLabels(), [
    public function filterAttributes()
        // HERE type your filter rules, like:
        // $this->query->andFilterWhere(['like', 'attribute', $this->attribute]);
        // ...

Extend your controller from \platx\rest\Controller, like this:

class MyController extends \platx\rest\Controller 
    public function actions()
        public $modelClass = '\common\models\MyModel';
        public $searchFormClass = '\common\forms\MySearchForm';
        return [
            'index' => [
                'class' => '\platx\rest\actions\IndexAction',
                'searchFormClass' => $this->searchFormClass
            'view' => [
                'class' => '\platx\rest\actions\ViewAction',
                'modelClass' => $this->modelClass
            'create' => [
                'class' => '\platx\rest\actions\CreateAction',
                'modelClass' => $this->modelClass
            'validate' => [
                'class' => '\platx\rest\actions\ValidateAction',
                'modelClass' => $this->modelClass
            'update' => [
                'class' => '\platx\rest\actions\UpdateAction',
                'modelClass' => $this->modelClass
            'delete' => [
                'class' => '\platx\rest\actions\DeleteAction',
                'modelClass' => $this->modelClass

In API Index action you can use query builder: /api/my?where[id][in]=1,2,3 or /api/my?where={"id":{"in":[1,2,3]}}

List of available operators:

Operator Value Description
eq One Parameter equals value
neq One Parameter not equals value
like One Value is a substring of parameter
nlike One Value is not a substring of parameter
in Many Attribute equals one of many values
nin Many Attribute not equals one of many values
gt One Attribute greater than value
gteq One Attribute greater than or equals value
lt One Attribute less than value
lteq One Attribute less than or equals value

In your config add this rules to UrlManager rules, if you want to use api like api module:

    'GET <module:api>/<controller:[\w-]+>' => '<module>/<controller>/index',
    'POST <module:api>/<controller:[\w-]+>' => '<module>/<controller>/create',
    'GET <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/view',
    'PUT <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/update',
    'DELETE <module:api>/<controller:[\w-]+>/<id:\d+>' => '<module>/<controller>/delete',
    'POST <module:api>/<controller:[\w-]+>/<action:validate>' => '<module>/<controller>/<action>',
    '<module:api>/<controller:[\w-]+>/<action:[\w-]+>/<id:\d+>' => '<module>/<controller>/<action>',
    '<module:api>/<controller:[\w-]+>/<action:[\w-]+>' => '<module>/<controller>/<action>',

And now you can use api, for example:

GET - List of records
GET - Select records from second to seventh
GET{"id": {"gteq": 2, "lt": 5}} - Select records with id greater than or equal 2 and less than 5
GET - List of records with sorting descending by created_at attribute
GET - List of records with sorting ascending by created_at attribute
GET - List of records with selecting just id attribute
GET,name - List of records with selecting just id and name attribute
GET - List of records with selecting fields value from extraFields array in our model
GET - Get one record by id attribute
POST - Create new record
POST,title - Validate specified fields.
PUT - Update record attributes
DELETE - Delete record