I’d do it this way.
Suppose you have the following generic routes on 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 draw your system in another way.)
In order for me to obtain a method or a collection of objects, I would search for a repository whose entity is mapped with 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 any data posting via HTTP.
You can also use the above standard to search for forms registered in the service container, as this way you would only need their service ID. This way your CRUD is already very generic. :)
I’ll give you +1 because I’m an admirer of [tag:symfony]
– Wallace Maxters