How to use the ParamConverter inside the controller or in service

2

The ParamConverter is a note I use constantly in Symfony to retrieve the data sent already in object, so the parse is made automatic, in my case, just add comment in the method of the respective controller and when receiving the parameter symfony he already sends me the object, already doing the reflection and parse necessary. A simple one.

/**
 * @ParamConverter("post", class="SensioBlogBundle:Post")
 */
public function showAction(Post $post)
{
}

And the $ post variable is already received as an object and fully populated, but what I need is something like:

/**
 * 
 */
public function showAction(Request $request, $classname, $entity)
{
    $post = new ParamConverter(array('class'=> $classname));
}

This is because I would like to implement a generic CRUD and in this case I do not know which class will come, it can be any class, does anyone know how it would be possible to do this?

    
asked by anonymous 18.05.2016 / 21:08

1 answer

0

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. :)

    
18.05.2016 / 23:00