Symfony route optional parameter

Symfony routes can have optional parameters, so that your path works with or without a parameter. This is something that usually a good service should do so that the user won’t get lost and no error pages occur.

This feature can be achieved by setting a default value for the parameter.

Here are the examples of setting routes in Symfony with default parameter.
Annotation:

YAML:

XML:

PHP:

By using this, you can access pages /task and /task/2 by using the same controller and route. Remember that trailing slash /task/ does not work by default on Symfony (you need to redirect trailing slashes).