8000 Show how to retrieve workflow in a class · symfony/symfony-docs@3e286a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e286a2

Browse files
amalricBzhwouterj
authored andcommitted
Show how to retrieve workflow in a class
1 parent 0c83d78 commit 3e286a2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

workflow.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,39 @@ 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+
In a class or a controller
220+
--------------------------
221+
222+
To access workflow inside a class, use dependency injection and inject the registry in the constructor.
223+
224+
For a controller, you can directly inject it in a method::
225+
226+
use Symfony\Component\Workflow\Registry;
227+
228+
class MyClass
229+
{
230+
231+
private $worflowRegistry;
232+
233+
public function __construct(Registry $workflowRegistry)
234+
{
235+
$this->worflowRegistry = $worflowRegistry;
236+
}
237+
238+
public function toReview(BlogPost $blogPost)
239+
{
240+
$workflow = $this->worflowRegistry->get($blogPost);
241+
242+
// Update the currentState on the post
243+
try {
244+
$workflow->apply($post, 'to_review');
245+
} catch (LogicException $exception) {
246+
// ...
247+
}
248+
// ...
249+
}
250+
}
218251

219252
Using Events
220253
------------

0 commit comments

Comments
 (0)
0