1
1
.. index ::
2
2
single: Workflow; Usage
3
3
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
6
13
7
14
A workflow is a process or a lifecycle that your objects go through. Each
8
15
step 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
14
21
a ``Definition `` and a way to write the states to the objects (i.e. an
15
22
instance of a :class: `Symfony\\ Component\\ Workflow\\ MarkingStore\\ MarkingStoreInterface `.)
16
23
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
19
26
like this:
20
27
21
28
.. configuration-block ::
22
29
23
30
.. code-block :: yaml
24
31
25
- # app/ config/config.yml
32
+ # config/packages/workflow.yaml
26
33
framework :
27
34
workflows :
28
35
blog_publishing :
@@ -51,7 +58,7 @@ like this:
51
58
52
59
.. code-block :: xml
53
60
54
- <!-- app/ config/config .xml -->
61
+ <!-- config/packages/workflow .xml -->
55
62
<?xml version =" 1.0" encoding =" utf-8" ?>
56
63
<container xmlns =" http://symfony.com/schema/dic/services"
57
64
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
@@ -98,7 +105,7 @@ like this:
98
105
99
106
.. code-block :: php
100
107
101
- // app/ config/config .php
108
+ // config/packages/workflow .php
102
109
103
110
$container->loadFromExtension('framework', array(
104
111
// ...
@@ -152,28 +159,43 @@ like this:
152
159
153
160
.. tip ::
154
161
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.
158
165
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::
161
170
162
- $post = new \App\Entity\BlogPost();
171
+ // ...
172
+ use Symfony\Component\Workflow\Registry;
163
173
164
- $workflow = $this->container->get('workflow.blog_publishing');
165
- $workflow->can($post, 'publish'); // False
166
- $workflow->can($post, 'to_review'); // True
174
+ class BlogController
175
+ {
176
+ public function edit(Registry $workflows)
177
+ {
178
+ $post = new \App\Entity\BlogPost();
179
+ $workflow = $workflows->get($post);
167
180
168
- // Update the currentState on the post
169
- try {
170
- $workflow->apply($post, 'to_review');
171
- } catch (LogicException $e) {
172
- // ...
173
- }
181
+ // if there are multiple workflows for the same class,
182
+ // pass the workflow name as the second argument
183
+ // $workflow = $workflows->get($post, 'blog_publishing');
174
184
175
- // See all the available transition for the post in the current state
176
- $transitions = $workflow->getEnabledTransitions($post);
185
+ $workflow->can($post, 'publish'); // False
186
+ $workflow->can($post, 'to_review'); // True
187
+
188
+ // Update the currentState on the post
189
+ try {
190
+ $workflow->apply($post, 'to_review');
191
+ } catch (LogicException $e) {
192
+ // ...
193
+ }
194
+
195
+ // See all the available transition for the post in the current state
196
+ $transitions = $workflow->getEnabledTransitions($post);
197
+ }
198
+ }
177
199
178
200
Using Events
179
201
------------
@@ -250,7 +272,8 @@ order:
250
272
* ``workflow.[workflow name].announce ``
251
273
* ``workflow.[workflow name].announce.[transition name] ``
252
274
253
- Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place::
275
+ Here is an example how to enable logging for every time the ``blog_publishing ``
276
+ workflow leaves a place::
254
277
255
278
use Psr\Log\LoggerInterface;
256
279
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
0 commit comments