Symfony route to annotation
When setting up routes with yml, xml or php you always define a name for a route. This name can be used to generate a url. With annotations this name is not required and not set in the documentation examples. The following is a example how a normal annotation comment looks like:
/**
* Transform item
*
* @param int $id ID of the item
* @return JsonResponse Json response
*
* @Route("/transform/item/{id}")
* @Method({"GET", "POST"})
*/
public function transformItemAction($id)
{
// Do transformation with item
}
By default the route still has a name, which is bundle_controller_action
. For the example above it would be liplex_backend_transformations_transformitem
.
You could then use it this way:
$url = $this->get('router')->generate(
'liplex_backend_transformations_transformitem',
['id'=> $id]
);
If you're not sure how the name is, you can see the route names when you run the following symfony console command:
$ php app/console router:debug
But there is also a way to set the name within the @Route
annotation and parameter name
like the following:
/**
* Transform item
*
* @param int $id ID of the item
* @return JsonResponse Json response
*
* @Route("/transform/item/{id}", name="transform_item")
* @Method({"GET", "POST"})
*/
public function transformItemAction($id)
{
// Do transformation with item
}