How can I have optional parameters in Symfony2 route?

You could also do it with a GET parameter, e.g.

/**
 * @param Request $request
 *
 * @return Response
 */
public function displayDetailAction(Request $request) : Response
{
    if ($courseId = $request->query->get('courseId')) {

Use a yml file for your routing configuration, and add a default value for id in your routing parameters like this:

user:
  pattern:   /site/user/{id}
  defaults:  { _controller: YourBundle:Default:index, id: 1 }

See documentation here


Try to go to site/user (notice no backslash at the end).

Generally it should work, I have relatively similar configuration working.

But if all else fails you can always define multiple routes for same action, i.e.

/**
 * Lists all User entities.
 *
 * @Route("/", name="user_no_cid")
 * @Route("/{cid}", name="user")
 * @Template()
 */
public function indexAction($cid=null)
{