I would do it as follows.
Suppose you have the following generic routes in your CRUD:
GET /{type} // lista os objetos de um determinado tipo
GET /{type}/new // exibe o formulário para criação de um objeto
GET /{type}/{id} // exibe um objeto
GET /{type}/{id}/edit // exibe o formulário para edição de um objeto
GET /{type}/{id}/delete // exibe o formulário para exclusão de um objeto
POST /{type} // cria um objeto
PUT /{type}/{id} // edita um objeto
DELETE /{type}/{id} // exclui um objeto
So these would be the methods of your controller :
public function listAction(Request $request, $type)
{
}
public function newAction(Request $request, $type)
{
}
public function getAction(Request $request, $type, $id)
{
}
public function getEditAction(Request $request, $type, $id)
{
}
public function getDeleteAction(Request $request, $type, $id)
{
}
public function postAction(Request $request, $type)
{
}
public function putAction(Request $request, $type, $id)
{
}
public function deleteAction(Request $request, $type, $id)
{
}
(ps: I like to split my actions so that each one has the minimum of responsibilities, you may want to design your system in another way.)
In order for me to get a method or a collection of objects, I would look for a repository whose entity is mapped to the value received in the URL:
private function getRepositoryName($type)
{
$mappings = [
'user' => AppBundle\Entity\User::class,
'item' => AppBundle\Entity\Item::class,
'order' => AppBundle\Entity\Order::class,
'address' => AppBundle\Entity\Order::class,
'phone' => AppBundle\Entity\Order::class,
]
returns $mappings[$type];
}
$repository = $this->getDoctrine->getRepository($this->getRepositoryName($type));
$entidade = $repository->find($id); // para buscar um único objeto
$entidades = $repository->findAll(); // para buscar uma coleção de objetos
From this, with the object in hand (or a collection of them), just use the object Request
received to process an eventual posting of data via HTTP.
You can also use the above pattern to fetch registered forms from the service container, since that way you would only need their service ID. That way your CRUD is already very generic. :)