2

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.

2 Answers 2

2
+50

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"})
Sign up to request clarification or add additional context in comments.

Comments

-2

Perhaps you can use 2 routes for the same method. One with the slug, one by default if not caught.

Or you can check with a basic if in the controller depending on the slug of your main issues .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.