8000 Merge branch '4.3' into 4.4 · symfony/symfony-docs@89f8e68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89f8e68

Browse files
committed
Merge branch '4.3' into 4.4
* 4.3: [#12545] Slightly improved the title Show how to retrieve workflow in a class
2 parents bae1123 + 0d3cf69 commit 89f8e68

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

workflow.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,38 @@ what actions are allowed on a blog post::
215215

216216
// See all the available transitions for the post in the current state
217217
$transitions = $workflow->getEnabledTransitions($post);
218+
219+
Accessing the Workflow in a Class
220+
---------------------------------
221+
222+
To access workflow inside a class, use dependency injection and inject the
223+
registry in the constructor::
224+
225+
use Symfony\Component\Workflow\Registry;
226+
227+
class MyClass
228+
{
229+
230+
private $worflowRegistry;
231+
232+
public function __construct(Registry $workflowRegistry)
233+
{
234+
$this->worflowRegistry = $worflowRegistry;
235+
}
236+
237+
public function toReview(BlogPost $blogPost)
238+
{
239+
$workflow = $this->worflowRegistry->get($blogPost);
240+
241+
// Update the currentState on the post
242+
try {
243+
$workflow->apply($post, 'to_review');
244+
} catch (LogicException $exception) {
245+
// ...
246+
}
247+
// ...
248+
}
249+
}
218250

219251
Using Events
220252
------------

0 commit comments

Comments
 (0)
0