phpsymfonysymfony4symfony-routing

Symfony Always Include Route Default Values


So basically I am trying to achieve this: New in Symfony 4.3: Always Include Route Default Values

I have a task entity, which has multiple states like new, open & closed.

For this reason I am trying to generate a route /tasks/new /tasks/open /tasks/closed. However, if there is no option set in my route, I want it to use new as default and also add it to the url.

My controller:

/**
 * @Route("/tasks/{!status?new}", name="app_tasks")
 */
public function tasks(TaskRepository $taskRepository, string $status)
{
    $tasks = $taskRepository->findBy(['status' => $status]);
    return $this->render('tasks/index.html.twig', ['tasks' => $tasks]);
}

However this results into not a single matching route.

/tasks/new  => no route found  => should work
/tasks/open => no route found  => should work
/tasks      => no route found  => should work & route should be /tasks/new
etc.

The main issue seems to be the ! before status. If I remove it entirely, everything works as expected, except that the route for new tasks looks like /tasks and not like /tasks/new - what I actually want.

I already tried to clear my cache - not working though. Symfony version is 4.4.2.


Solution

  • There is an open issue on github. This will likely be fixed in newer releases.

    As for now try using the following:

    @Route("/tasks/{!status}", name="app_tasks", defaults={"status": "new"})