yii2-workflow A simple workflow engine for Yii2

  1. Installation
  2. Configuration
  3. Create A Workflow
  4. Attach To The Model
  5. Use It !
  6. Resources

The yii2-workflow extension is a set of Yii2 components dedicated to provide an easy way to manage the life cycle of an ActiveRecord objects inside a workflow.

For those who used the simpleWorkflow with Yii 1.x, you'll be familiar with this Yii2 version. For the other ones, read what follows, welcome to the wonderful world of workflows !


The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist raoul2000/yii2-workflow "*"

or add

"raoul2000/yii2-workflow": "*"

to the require section of your composer.json file.


For this "Quick start Guide" we will be using default configuration settings, but note that yii2-workflow is designed to be highly flexible so to adapt to a lot of execution contexts... well at least that was my goal.

Create A Workflow

A workflow is defined as a PHP class that implements the \raoul2000\workflow\source\file\IWorkflowDefinitionProvider interface. This interface declares the getDefinition() method that must return an array representing the workflow.

Let's define a very simple workflow that will be used to manage posts in a basic blog system.


Here is the PHP class that implements the definition for our workflow :


namespace app\models;

class PostWorkflow implements \raoul2000\workflow\source\file\IWorkflowDefinitionProvider 
	public function getDefinition() {
		return [
			'initialStatusId' => 'draft',
			'status' => [
				'draft' => [
					'transition' => ['publish','deleted']
				'publish' => [
					'transition' => ['draft','deleted']
				'deleted' => [
					'transition' => ['draft']

Attach To The Model

Now let's have a look to our Post model: we store the status of a post in a column named status.

The last step is to associate the workflow definition with posts models. To do so we must declare the SimpleWorkflowBehavior behavior in the Post model class and let the default configuration settings do the rest.


namespace app\models;
 * @property integer $id
 * @property string $title
 * @property string $body
 * @property string $status column used to store the status of the post
class Post extends \yii\db\ActiveRecord
    public function behaviors()
    	return [
    // ...

That's it ! We are ready to play with SimpleWorkflowBehavior.

Use It !

Now that we are all setup, we can use the SimpleWorkflowBehavior methods to set/get the status of our posts : the SimpleWorkflowBehavior will take care that the post doesn't reach a status where it is not supposed to go, depending on the workflow definition that we have created.

$post = new Post();
$post->status = 'draft';
echo 'post status is : '. $post->workflowStatus->label;

This will print the following message :

post status is : Draft

That's it ! .. well not exactly : there is much more : you need to read the workflow definition from a PHP array ? from a grahml file created by yEd ? you would like to defines events handlers to manage you models ? implement constraints, tasks and plenty of cool other thing ? .. well, have fun with yii2-workflow !


0 0
Yii Version: 2.0.*
License: BSD-2-Clause
Category: Others
Developed by: Raoul
Created on: Oct 14, 2015
Last updated: 5 years ago
Github Repository

Related Extensions