PHPackages                             conceptho/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. [Framework](/categories/framework)
4. /
5. conceptho/yii2-state-machine

ActiveYii2-extension[Framework](/categories/framework)

conceptho/yii2-state-machine
============================

Yii2 State Machine

0.1.0(9y ago)62.0k4PHP

Since Jul 5Pushed 5y ago5 watchersCompare

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

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/5fd6ec0f44d6ebedb417167e211a3678413e74ad6125644524a4aa1c76e0675e/68747470733a2f2f706f7365722e707567782e6f72672f636f6e63657074686f2f796969322d73746174652d6d616368696e652f762f737461626c65)](https://packagist.org/packages/conceptho/yii2-state-machine)[![Total Downloads](https://camo.githubusercontent.com/1887362c05d2e3b3c24eabe1a531d757e1db65a90dca6629d0afe5de42c5edf3/68747470733a2f2f706f7365722e707567782e6f72672f636f6e63657074686f2f796969322d73746174652d6d616368696e652f646f776e6c6f6164732e706e67)](https://packagist.org/packages/conceptho/yii2-state-machine)

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

[](#installation)

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

Either run

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

```

or add

```
"conceptho/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 {

    public function modelLabel() {
        return 'User';
    }

    public function behaviors() {
        return \yii\helpers\ArrayHelper::merge(parent::behvaiors(), [
            [
                'class' => conceptho\state\Machine::class,
                'initial' => 'active', /// Initial status
                'attr' => 'status', /// Attribute that will use this state machine
                'namespace' => 'app\models\status\user', /// Namespace for the Status class definitions
                'model_label' => $this->modelLabel(),
                'transitions' => [
                    'active' => ['inactive', 'disabled'],
                    'inactive' => ['active', 'disabled'],
                    'disabled' => ['inactive']
                ]
            ]
        ]);
    }
}
```

Status definitions:

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

use conceptho\state\Status;

class Active extends Status {
    public const ID = 'active';
    public $label = 'Active';
    public $labelColor = 'primary';

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

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

}
```

```
/// Inactive Status
namespace app\models\status\user;

use conceptho\state\Status;

class Inactive extends Status {
    public const ID = 'inactive';
    public $label = 'Inactive';
    public $labelColor = 'danger';

    public function onExit($id, $event)
    {
        /// event triggered when the status is changed from Inactive to another status
        return true;
    }

    public function onEntry($id, $event)
    {
        /// event triggered when the status is changed from another status to Inactive
        return true;
    }

}
```

```
/// Disabled Status
namespace app\models\status\user;

use conceptho\state\status;

class Disabled extends Status {
    public const ID = 'disabled';
    public $label = 'Disabled';
    public $labelColor = 'muted';

    public function onExit($id, $event)
    {
        /// event triggered when the status is changed from Disabled to another status
        return true;
    }

    public function onEntry($id, $event)
    {
        /// event triggered when the status is changed from another status to Disabled
        return true;
    }

}
```

### 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

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

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

3598d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/636229?v=4)[Andre Metzen](/maintainers/andremetzen)[@andremetzen](https://github.com/andremetzen)

---

Top Contributors

[![pedromalta](https://avatars.githubusercontent.com/u/3971810?v=4)](https://github.com/pedromalta "pedromalta (11 commits)")[![andremetzen](https://avatars.githubusercontent.com/u/636229?v=4)](https://github.com/andremetzen "andremetzen (4 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)")[![ezsky](https://avatars.githubusercontent.com/u/383284?v=4)](https://github.com/ezsky "ezsky (1 commits)")

### Embed Badge

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

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

###  Alternatives

[skeeks/cms

SkeekS CMS — control panel and tools based on php framework Yii2

13825.6k47](/packages/skeeks-cms)

PHPackages © 2026

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