I'm trying to implement a controller that operates a recursion on an array. Here is the code:
/**
* @Route("/printTree", name="printTree")
*/
public function printTree(array $elements, $parentId = 0) {
$em = $this->getDoctrine()->getManager();
$elements = $em->getRepository('AppBundle:Tree')->findAll();
$treeArray = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = printTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$treeArray[] = $element;
}
}
return $treeArray;
}
This is the error I get:
Controller "AppBundle\Controller\DefaultController::printTree()" requires that you provide a value for the "$elements" argument (because there is no default value or because there is a non optional argument after this one).
I searched through the website for other similar issues, and the problem seems to be in the Doctrine annotations, where placeholders are needed. If I write for example:
/**
* @Route("/printTree/{$elements}/{0}", name="printTree")
*/
how can I make it work in this example?
$this->printTreeinstead ofprintTree