From 3e286a2ca9ae11ce83c115ce37cde433ba8a31e2 Mon Sep 17 00:00:00 2001 From: AmalricBzh Date: Fri, 25 Oct 2019 23:07:29 +0200 Subject: [PATCH] Show how to retrieve workflow in a class --- workflow.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/workflow.rst b/workflow.rst index 26f64be175b..368eada8da8 100644 --- a/workflow.rst +++ b/workflow.rst @@ -215,6 +215,39 @@ what actions are allowed on a blog post:: // See all the available transitions for the post in the current state $transitions = $workflow->getEnabledTransitions($post); + +In a class or a controller +-------------------------- + +To access workflow inside a class, use dependency injection and inject the registry in the constructor. + +For a controller, you can directly inject it in a method:: + + use Symfony\Component\Workflow\Registry; + + class MyClass + { + + private $worflowRegistry; + + public function __construct(Registry $workflowRegistry) + { + $this->worflowRegistry = $worflowRegistry; + } + + public function toReview(BlogPost $blogPost) + { + $workflow = $this->worflowRegistry->get($blogPost); + + // Update the currentState on the post + try { + $workflow->apply($post, 'to_review'); + } catch (LogicException $exception) { + // ... + } + // ... + } + } Using Events ------------