Download the PHP package hjp1011/yii2-workflow without Composer
On this page you can find all versions of the php package hjp1011/yii2-workflow. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hjp1011/yii2-workflow
More information about hjp1011/yii2-workflow
Files in hjp1011/yii2-workflow
Package yii2-workflow
Short Description A simple workflow engine for Yii2
License BSD-3-Clause
Homepage https://github.com/hjp1011/yii2-workflow
Informations about the package yii2-workflow
yii2-workflow
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Quick Start
Configuration
For this "Quick start Guide" we will be using default configuration settings, but remember 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 \hjp1011\workflow\source\file\IWorkflowDefinitionProvider
interface. which
declares the getDefinition() method. This method must return an array representing the workflow definition.
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 :
@app/models/PostWorkflow.php
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
of type STRING(40).
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.
@app/models/Post.php
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 to, depending on the workflow definition that we have provided.
This will print the following message :
post status is : Draft
If you do the same thing but instead of draft set the status to publish and try to save it, the following exception is thrown :
Not an initial status : PostWorkflow/publish ("PostWorkflow/draft" expected)
That's because in your workflow definition the initial status is set to draft and not publish.
Ok, one more example for the fun ! This time we are not going to perform the transition when the Post is saved (like we did in the previous
example), but immediately, by invoking the sendToStatus
method. Our Post is going to try to reach status publish passing through deleted
which is strictly forbidden by the workflow. Will it be successful in this risky attempt to break workflow rules ?
Game Over ! There is no transition between deleted and publish, and that's what SimpleWorkflow tries to explain to our fearless post object.
Workflow Exception – hjp1011\workflow\base\WorkflowException
No transition found between status PostWorkflow/deleted and PostWorkflow/publish
Yes, that's severe, but there was many ways to avoid this exception like for instance by first validating that the transition was possible.
What's Next ?
This is just one way of using the SimpleWorkflowBehavior but there's much more and hopefully enough to assist you in workflow management inside your Yii2 web app.
You may also be interested in the following projects developed around yii2-workflow :
- yii2-workflow-view : A Widget to display workflows
License
yii2-workflow is released under the BSD 3-Clause License. See the bundled LICENSE.md
for details.