PHPackages                             ddruganov/yii2-api-essentials - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ddruganov/yii2-api-essentials

ActiveLibrary

ddruganov/yii2-api-essentials
=============================

Bunch of components to make one's development life eaisier

1.0.11(4y ago)0482PHP

Since Feb 4Pushed 4y ago1 watchersCompare

[ Source](https://github.com/ddruganov/yii2-api-essentials)[ Packagist](https://packagist.org/packages/ddruganov/yii2-api-essentials)[ RSS](/packages/ddruganov-yii2-api-essentials/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (13)Used By (2)

yii2-api-essentials
===================

[](#yii2-api-essentials)

Bunch of small components to make one's yii2 api development life eaiser

Installation
------------

[](#installation)

`composer require ddruganov/yii2-api-essentials`

Description
-----------

[](#description)

This package contains useful classes (filters, behaviors, helpers, etc) for api development:

- `behaviors\TimestampBehavior`: provides basic timestamping of an active record model with fields being 'created\_at' and 'updated\_at' (detects their existence automatically)
- `collectors\AbstractDataCollector`: a convient way to collect data for an api call
- `exceptions\ModelNotFoundException`: when a search for a model in database fails
- `exceptions\NotImplementedException`: when you need to write an implementation for a method but... later :D
- `forms\Form`: a container that performs validation of data and manipulates in a certain way; super useful for creating models without directly using an ActiveRecord; also used as a base for data collectors
- `http\actions\ApiAction`: base class for all api actions that provides a way to get all incoming data as one array; returns an `ExecutionResult` and a status code appropriate for that execution result;
- `http\actions\ClosureAction`: when the `ApiModelAction` is too overkill
- `http\actions\FormAction`: provides a way to separate validation and saving of data to reduce class bloat; uses `Form`
- `http\controllers\ApiController`: returns everything as json, measures every request timing and makes all api calls transactional
- `http\filters\TimerFilter`: measures the time that an action takes and dumps into the debug log
- `http\filters\TransactionFilter`: starts transaction before action, ends it after action; depends on `ExecutionResult` being successful
- `testing\UnitTest`: base unit test class; provides a convenient way to assert `ExecutionResult` state and a customizable faker generator
- `traits\Activity`: used in `ActiveQuery` to only get active models
- `traits\Pagination`: used in `ActiveQuery` to set a page in a list query and to get a page count
- `traits\Sorting`: used in `ActiveQuery` to sort models; deafult field is `created_at`
- `traits\SoftDelete`: used in `ActiveRecord`, when a model is deleted the `deleted_at` field is filled with `date('Y-m-d H:i:s')`
- `DateHelper`: bunch of useful methods for date manipulation
- `ExecutionResult`: basically a statically typed version of an array with a specific structure to ensure that all methods working with data return the same structure, but not limited to that

ApiController example
---------------------

[](#apicontroller-example)

```
class TestController extends ApiController
{
    public function actions()
    {
        return [
            'test1' => [
                'class' => ClosureAction::class,
                'closure' => function () {
                    Yii::debug('this is a closure action');
                    return ExecutionResult::success();
                }
            ],
            'test2' => [
                'class' => FormAction::class,
                'formClass' => TestCollector::class
            ],
            'test3' => [
                'class' => FormAction::class,
                'formClass' => TestForm::class
            ]
        ];
    }
}
```

`TestCollector` and `TestForm` extend the `forms\Form`

ActiveQuery traits example
--------------------------

[](#activequery-traits-example)

```
class SomeModelQuery extends ActiveQuery {
    use Activity, Pagination, Sorting;
}
```

ActiveRecord-related components example
---------------------------------------

[](#activerecord-related-components-example)

```
class SomeActiveRecord extends ActiveRecord {

    use SoftDelete;

    public function behaviors() {
        return [TimestampBehavior::class];
    }
}
```

Form example
------------

[](#form-example)

```
class SomeModelCreationForm extends Form {
    public ?string $name = null;
    public ?array $someRelatedModelIds = null;

    public function rules() {
        return [
            [['name','someRelatedModelIds'],'required'],
            [['name'],'string'],
            [['someRelatedModelIds'],'each', 'rule' => ['integer']]
        ];
    }

    protected function _run(): ExecutionResult {
        $model = new SomeModel();
        $model->setAttributes(['name' => $this->name]);
        if (!$model->save()) {
            return ExecutionResult::exception('Error saving model');
        }

        $result = $this->saveRelatedModelIds($model);
        if (!$result->isSuccessful()){
            return $result;
        }

        return ExecutionResult::success([
            'id' => $model->getId()
        ]);
    }

    private function saveRelatedModelIds(SomeModel $model) {
        ... bind related model ids here
    }
}
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

12

Last Release

1482d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b68509a8d66d630b4f6a4faef3a5cb2cbea6b5c78321cc6940aa78554a95a27a?d=identicon)[ddruganov](/maintainers/ddruganov)

---

Top Contributors

[![ddruganov](https://avatars.githubusercontent.com/u/12191677?v=4)](https://github.com/ddruganov "ddruganov (32 commits)")

### Embed Badge

![Health badge](/badges/ddruganov-yii2-api-essentials/health.svg)

```
[![Health](https://phpackages.com/badges/ddruganov-yii2-api-essentials/health.svg)](https://phpackages.com/packages/ddruganov-yii2-api-essentials)
```

###  Alternatives

[magento/magento2-functional-testing-framework

Magento2 Functional Testing Framework

15511.5M30](/packages/magento-magento2-functional-testing-framework)[orchestra/workbench

Workbench Companion for Laravel Packages Development

8017.0M43](/packages/orchestra-workbench)[elgg/elgg

Elgg is an award-winning social networking engine, delivering the building blocks that enable businesses, schools, universities and associations to create their own fully-featured social networks and applications.

1.7k15.7k5](/packages/elgg-elgg)[worksome/foggy

Foggy is a tool for making database dumps with some data removed/changed.

26571.7k1](/packages/worksome-foggy)[guncha25/drupal-codeception

Codeception toolset for Drupal testing.

17131.1k2](/packages/guncha25-drupal-codeception)[sylius/core-bundle

Sylius core bundle. It integrates all other bundles into full stack Symfony ecommerce solution.

22100.0k46](/packages/sylius-core-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
