Search code examples
phpsymfonyurl-routing

How to route differently a request if param empty


On Symfony 4.3, I'm trying to route differently a request like below:

index:
  path: /{param}
  controller: App\Controller\MyController::doSomethiing
home:
  path: /
  controller: App\Controller\HomeController::home

But if I try to access /, the programm enter MyController::doSomething with an empty parameter.

I did what I want by checking if param is empty in the MyController::doSomething method but I don't think it is the proper way.

Is there a way to do this from the routes.yaml file?


Solution

  • Okay, found it finally. It's just about the order. The right configuration file is:

    home:
      path: /
      controller: App\Controller\HomeController::home
    index:
      path: /{param}
      controller: App\Controller\MyController::doSomething