Warning: You are browsing the documentation for Symfony 4.2, which is no longer maintained.
Read the updated version of this page for Symfony 5.3 (the current stable version).
The Workflow Component
The Workflow Component¶
The Workflow component provides tools for managing a workflow or finite state machine.
$ composer require symfony/workflow
If you install this component outside of a Symfony application, you must
vendor/autoload.php file in your code to enable the class
autoloading mechanism provided by Composer. Read
this article for more details.
Creating a Workflow¶
The workflow component gives you an object oriented way to define a process or a life cycle that your object goes through. Each step or stage in the process is called a place. You do also define transitions that describe the action to get from one place to another.
A set of places and transitions creates a definition. A workflow needs
Definition and a way to write the states to the objects (i.e. an
instance of a
Consider the following example for a blog post. A post can have one of a number of predefined statuses (draft, review, rejected, published). In a workflow, these statuses are called places. You can define the workflow like this:
use Symfony\Component\Workflow\DefinitionBuilder; use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore; use Symfony\Component\Workflow\Transition; use Symfony\Component\Workflow\Workflow; $definitionBuilder = new DefinitionBuilder(); $definition = $definitionBuilder->addPlaces(['draft', 'review', 'rejected', 'published']) // Transitions are defined with a unique name, an origin place and a destination place ->addTransition(new Transition('to_review', 'draft', 'review')) ->addTransition(new Transition('publish', 'review', 'published')) ->addTransition(new Transition('reject', 'review', 'rejected')) ->build() ; $marking = new SingleStateMarkingStore('currentState'); $workflow = new Workflow($definition, $marking);
Workflow can now help you to decide what actions are allowed
on a blog post depending on what place it is in. This will keep your domain
logic in one place and not spread all over your application.
When you define multiple workflows you should consider using a
which is an object that stores and provides access to different workflows.
A registry will also help you to decide if a workflow supports the object you
are trying to use it with:
use Acme\Entity\BlogPost; use Acme\Entity\Newsletter; use Symfony\Component\Workflow\Registry; use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy; $blogWorkflow = ... $newsletterWorkflow = ... $registry = new Registry(); $registry->addWorkflow($blogWorkflow, new InstanceOfSupportStrategy(BlogPost::class)); $registry->addWorkflow($newsletterWorkflow, new InstanceOfSupportStrategy(Newsletter::class));
When you have configured a
Registry with your workflows,
you can retreive a workflow from it and use it as follows:
// ... // Consider that $post is in state "draft" by default $post = new BlogPost(); $workflow = $registry->get($post); $workflow->can($post, 'publish'); // False $workflow->can($post, 'to_review'); // True $workflow->apply($post, 'to_review'); // $post is now in state "review" $workflow->can($post, 'publish'); // True $workflow->getEnabledTransitions($post); // ['publish', 'reject']
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.