Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Accessing route variables

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?

like image 625
acid Avatar asked Jul 31 '12 09:07

acid


3 Answers

You can get all route related parameters from the Request

$routeParams = $request->attributes->get('_route_params');
$id = $routeParams['id'];
like image 117
Vitalii Zurian Avatar answered Nov 06 '22 05:11

Vitalii Zurian


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

like image 9
acid Avatar answered Nov 06 '22 04:11

acid


You can do

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

The $id variable will be available within the controller.

like image 3
Leo Avatar answered Nov 06 '22 03:11

Leo