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?
You can get all route related parameters from the Request
$routeParams = $request->attributes->get('_route_params');
$id = $routeParams['id'];
$request->attributes->get('id')
does the trick.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With