-1

I would like to merge two routes with ParamConverters on Symfony 5. On the root route I want to get all the records from the Entity table. On the id route I want to get only that particular Id. But as I access the root route i get this Exception: "Unable to guess how to get a Doctrine instance from the request information for parameter "cronjob". "

/**
 * @Route("", methods="GET")
 * @Route("{id}", methods="GET")
 */
public function restGet(Cronjob $cronjob)
{
    ...//logic for getting all or one record
}
Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78
  • This doesn't make much sense, IMO If you you want to use the param converter and type hint for `Cronjobs`, logically you want (you **need**) a `Cronjob` instance to be able to call this method. – yivi Nov 19 '20 at 06:36
  • Cronjobs is not an array of entities ..It's 1 entity. Sorry but the plural was not meant to be there. – Claudio Ferraro Nov 19 '20 at 09:24
  • You say "on the root route I want to get all the records from the entity table". And you say that you want to use this controller for that. Doesn't make sense. This controller expects the ParamConverter to instantiate a single `Cronjob`. – yivi Nov 19 '20 at 09:29

1 Answers1

0

I solved it in this way

/**
 * @ApiRoute("{id?}", methods="GET")
 */
 public function restGet($id, Cronjobs $cronjob = null)
 {
     return $this->json(isset($id) ? $this->getRepository()->findAll() : $cronjob);
 }

The ParamConverter gets called implicitly and the $cronjob populated or not.

It's important the null on $cronjob and the nullable parameter : {id?} with question mark.

I extend from AbstractController

Claudio Ferraro
  • 4,551
  • 6
  • 43
  • 78