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,46 @@ 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;
173
+ use App\Entity\BlogPost;
174
+ use Symfony\Bundle\FrameworkBundle\Controller\Controller;
175
+ use Symfony\Component\Workflow\Exception\LogicException;
163
176
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);
167
183
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');
174
187
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
+ }
177
202
178
203
Using Events
179
204
------------
@@ -250,7 +275,8 @@ order:
250
275
* ``workflow.[workflow name].announce ``
251
276
* ``workflow.[workflow name].announce.[transition name] ``
252
277
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::
254
280
255
281
use Psr\Log\LoggerInterface;
256
282
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
0 commit comments