Symfony2 Accessing route variables

Viewed 12020

I know I can access current route name by $request->get('_route');.

If my route is defined this way:

/*
 * @Route("/get_by_category/{id}", defaults={"id" = 0}, name="get_products_by_category")
 */

How can I retrieve the id variable from within service?

4 Answers

In Symfony 3.4, I just added function functionName(int $id) and the variable $id has defined in the function scope. My path was something/{id}. For multiple parameters like/{id}/that.{format} I just added the second parameter to the function: function functionName(int $id, string $format), and both of them are in the scope. Indeed, my functionName is a __invoke in a class used as controller to the route.

Related