11..index ::
22 single: Workflow; Usage
33
4- How to Use the Workflow
5- =======================
4+ How to Create and Use Workflows
5+ ===============================
6+
7+ Before creating your first workflow, execute this command to install the
8+ :doc: `Workflow component </components/workflow >` in your application:
9+
10+ ..code-block ::terminal
11+
12+ $ composer require workflow
613
714 A workflow is a process or a lifecycle that your objects go through. Each
815step or stage in the process is called a *place *. You do also define *transitions *
@@ -14,15 +21,15 @@ A set of places and transitions creates a **definition**. A workflow needs
1421a ``Definition `` and a way to write the states to the objects (i.e. an
1522instance of a:class: `Symfony\\ Component\\ Workflow\\ MarkingStore\\ MarkingStoreInterface `.)
1623
17- Consider the following example for a blog post. A post can have places:
18- ' draft', ' review', ' rejected', ' published' . You can define the workflow
24+ Consider the following example for a blog post that can have these places:
25+ `` draft ``, `` review ``, `` rejected ``, `` published `` . You can define the workflow
1926like this:
2027
2128..configuration-block ::
2229
2330 ..code-block ::yaml
2431
25- # app/ config/config.yml
32+ # config/packages/workflow.yaml
2633framework :
2734workflows :
2835blog_publishing :
@@ -51,7 +58,7 @@ like this:
5158
5259 ..code-block ::xml
5360
54- <!-- app/ config/config .xml-->
61+ <!-- config/packages/workflow .xml-->
5562 <?xml version =" 1.0" encoding =" utf-8" ?>
5663 <container xmlns =" http://symfony.com/schema/dic/services"
5764xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -98,7 +105,7 @@ like this:
98105
99106 ..code-block ::php
100107
101- //app/ config/config .php
108+ // config/packages/workflow .php
102109
103110 $container->loadFromExtension('framework', array(
104111 // ...
@@ -152,28 +159,46 @@ like this:
152159
153160..tip ::
154161
155- The ``type `` (default value ``single_state ``) and ``arguments `` (default value `` marking ``)
156- attributes of the ``marking_store `` option are optional. If omitted, their default values
157- will be used.
162+ The ``type `` (default value ``single_state ``) and ``arguments `` (default
163+ value `` marking ``) attributes of the ``marking_store `` option are optional.
164+ If omitted, their default values will be used.
158165
159- With this workflow named ``blog_publishing ``, you can get help to decide
160- what actions are allowed on a blog post::
166+ With this workflow named ``blog_publishing ``, you can now decide what actions
167+ are allowed on a blog post. For example, inside a controller of an application
168+ using the:ref: `default services.yaml configuration <service-container-services-load-example >`,
169+ you can get the workflow by injecting the Workflow registry service::
161170
162- $post = new \App\Entity\BlogPost();
171+ // ...
172+ use Symfony\Component\Workflow\Registry;
173+ use App\Entity\BlogPost;
174+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
175+ use Symfony\Component\Workflow\Exception\LogicException;
163176
164- $workflow = $this->container->get('workflow.blog_publishing');
165- $workflow->can($post, 'publish'); // False
166- $workflow->can($post, 'to_review'); // True
177+ class BlogController extends Controller
178+ {
179+ public function edit(Registry $workflows)
180+ {
181+ $post = new BlogPost();
182+ $workflow = $workflows->get($post);
167183
168- // Update the currentState on the post
169- try {
170- $workflow->apply($post, 'to_review');
171- } catch (LogicException $e) {
172- // ...
173- }
184+ // if there are multiple workflows for the same class,
185+ // pass the workflow name as the second argument
186+ // $workflow = $workflows->get($post, 'blog_publishing');
174187
175- // See all the available transition for the post in the current state
176- $transitions = $workflow->getEnabledTransitions($post);
188+ $workflow->can($post, 'publish'); // False
189+ $workflow->can($post, 'to_review'); // True
190+
191+ // Update the currentState on the post
192+ try {
193+ $workflow->apply($post, 'to_review');
194+ } catch (LogicException $e) {
195+ // ... if the transition is not allowed
196+ }
197+
198+ // See all the available transitions for the post in the current state
199+ $transitions = $workflow->getEnabledTransitions($post);
200+ }
201+ }
177202
178203Using Events
179204------------
@@ -250,7 +275,8 @@ order:
250275 * ``workflow.[workflow name].announce ``
251276 * ``workflow.[workflow name].announce.[transition name] ``
252277
253- Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place::
278+ Here is an example of how to enable logging for every time the ``blog_publishing ``
279+ workflow leaves a place::
254280
255281 use Psr\Log\LoggerInterface;
256282 use Symfony\Component\EventDispatcher\EventSubscriberInterface;