This post will cover how to use a ProcessManager class together with Symfony’s workflow component.
The Process Manager
The ProcessManager is responsible for mapping states with controller routes. This makes it easy for you to know which
controller action that is next in line and ready to be executed.
The ProcessManager class looks like this:
A workflow is very similar to a state machine, containing places and transitions, where places refer to “states”.
To read more about configuring and setting up your own workflow, please see the Symfony documentation
Solution
The first thing to do in your controller would be to retrieve your state machine from the service container:
Next, check if the transition you want to apply is available for your object, based on its current state.
If it is, apply it and let Doctrine update the database:
Now comes the interesting part:
After applying the new state, you would also want to redirect the user to the next step. This is where the ProcessManager comes in.
Before you can start using the ProcessManager you have to declare it in services.yml:
The argument sent to the ProcessManager is the specific workflow that we want our ProcessManager to handle.
As we can see in the declaration above, each state has a route name assigned to it. For an example:
The state create_account is mapped to the 'account_register' route name.
Each route name refers to a specific
controller action, and this is handled by annotations.
With the state-to-route mapping, the ProcessManager will always make sure that the users is redirected to the correct controller.
A final example of a controller action would look like this:
The Symfony Runtime component is AWESOME. I did a talk about it at
Symfony World 2021 where I explain how and
why it works. I spent a lot of time on the cont...
The lock component have saved me so many times. It helps me with race conditions,
it makes my code simpler and my application more reliable. I’m using it to ...
I’ve been blogging now and then for over a decade now. I have used Drupal, Wordpress,
plain HTML files, static generators as Jekyll and Sculpin. I’ve also us...
It has been one year since Jérémy Derussé and I started to work on a new API client
for AWS. At the time we could never have imagined how popular it would be...
Leave a Comment