Symfony2 Access Route Options

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

If my route is defined as follows:

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

How can I get a variable idfrom a service?

+5
source share
3 answers

$request->attributes->get('id') does the trick.

+5
source

You can get all the parameters related to the route from Request

$routeParams = $request->attributes->get('_route_params');
$id = $routeParams['id'];
+9
source

You can do

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

A variable will be available in the controller $id.

+3
source

All Articles