PHPackages                             bestyii/yii2-state-machine - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. bestyii/yii2-state-machine

ActiveYii2-extension[Utility &amp; Helpers](/categories/utility)

bestyii/yii2-state-machine
==========================

Yii2 State Machine

0.0.1(6y ago)027PHP

Since Mar 27Pushed 6y agoCompare

[ Source](https://github.com/bestyii/yii2-state-machine)[ Packagist](https://packagist.org/packages/bestyii/yii2-state-machine)[ RSS](/packages/bestyii-yii2-state-machine/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Yii2 State Machine
==================

[](#yii2-state-machine)

This package enable state machine usage into attributes of a Model (Active Record).

[![Latest Stable Version](https://camo.githubusercontent.com/6472f4101c1dbe213a8740a659537d8e1a4f7b76aed2e7fb59c3d97e64998a40/68747470733a2f2f706f7365722e707567782e6f72672f626573747969692f796969322d73746174652d6d616368696e652f762f737461626c65)](https://packagist.org/packages/bestyii/yii2-state-machine)[![Total Downloads](https://camo.githubusercontent.com/9f8fbcba3713353f183deab4e431fdf8682deabfc65b811cd463de80a2152325/68747470733a2f2f706f7365722e707567782e6f72672f626573747969692f796969322d73746174652d6d616368696e652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/bestyii/yii2-state-machine)

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require bestyii/yii2-state-machine

```

or add

```
"bestyii/yii2-state-machine": "dev-master"
```

to the `require` section of your composer.json.

Usage
-----

[](#usage)

Model definition:

```
/// Model

namespace app\models;

class User extends \yii\db\ActiveRecord {
    const  STATUS_ACTIVE = 'ACTIVE';
    const  STATUS_INACTIVE = 'INACTIVE';
    const  STATUS_DISABLED = 'DISABLED';
    public function modelLabel() {
        return 'User';
    }

    public function behaviors() {
        return \yii\helpers\ArrayHelper::merge(parent::behvaiors(), [
            [
                'class' => bestyii\state\Machine::class,
                'initial' => self::STATUS_PENDING, /// Initial status
                'attr' => 'status', /// Attribute that will use this state machine
                'model_label' => $this->modelLabel(),
                'transitions' => [
                    Active::className(),
                    Inactive::className(),
                    Disabled::className()
                ]
            ]
        ]);
    }
}
```

Status definitions:

```
/// Active status
namespace app\models\status\user;

use bestyii\state\Status;

class Active extends Status {
    public $id = User::STATUS_ACTIVE;
    public $label = 'Active';
    public $labelColor = 'primary';
    public static $availableStatus = [User::STATUS_INACTIVE, User::STATUS_DISABLED];

    public function canChangeTo($id,$model){
        return true;
    }

    public function onExit($id, $event,$model)
    {
        /// event triggered when the status is changed from Active to another status
    }

    public function onEntry($id, $event,$model)
    {
        /// event triggered when the status is changed from another status to Active
    }

}
```

### Example:

[](#example)

```
$user = new User();
/// Returns the current status: new Active()
$user->status;
/// Returns the allowed status IDs that can be changed to in this case: ['inactive', 'disabled']
$user->allowedStatusChanges();

/// Returns a boolean value in this case: true
$user->canChangeTo('inactive');
/// in this case: false. Since this status is not defined in the transitions key values.
$user->canChangeTo('unknown');
/// Returns all the defined Status in the Model, in this case:
/// ['active' => new Active(), 'inactive' => new Inactive(), 'disabled' => new Disabled()]
$user->availableStatus();

/// Change from Active to Inactive triggering the events onEntry of inactive and onExit of Active
$user->changeTo('inactive');

/// Returns the current status: new Inactive()
$user->status;

/// Change from Inactive to Disabled triggering the events onExit of inactive and onEntry of Disabled
$user->changeTo('disabled');

/// Throws a error since disabled cant be changed to active.
$user->changeTo('active');
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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

Unknown

Total

1

Last Release

2237d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/383284?v=4)[Yang](/maintainers/ezsky)[@ezsky](https://github.com/ezsky)

---

Top Contributors

[![pedromalta](https://avatars.githubusercontent.com/u/3971810?v=4)](https://github.com/pedromalta "pedromalta (11 commits)")[![ezsky](https://avatars.githubusercontent.com/u/383284?v=4)](https://github.com/ezsky "ezsky (5 commits)")[![andremetzen](https://avatars.githubusercontent.com/u/636229?v=4)](https://github.com/andremetzen "andremetzen (3 commits)")[![engylemure](https://avatars.githubusercontent.com/u/26777018?v=4)](https://github.com/engylemure "engylemure (2 commits)")[![bazaglia](https://avatars.githubusercontent.com/u/6966980?v=4)](https://github.com/bazaglia "bazaglia (1 commits)")

### Embed Badge

![Health badge](/badges/bestyii-yii2-state-machine/health.svg)

```
[![Health](https://phpackages.com/badges/bestyii-yii2-state-machine/health.svg)](https://phpackages.com/packages/bestyii-yii2-state-machine)
```

###  Alternatives

[dmstr/yii2-cookie-consent

Yii2 Cookie Consent Widget

1452.6k](/packages/dmstr-yii2-cookie-consent)

PHPackages © 2026

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