How to use Paramconverter inside the controller or in service

Asked

Viewed 45 times

2

The Paramconverter is a note that I use constantly in Symfony to recover the data sent already in object, so parse is made automatic, in my case, simply add comment in the method of the respective controller and when receiving the parameter symfony it already sends me the object, already doing the required Reflection and parse. A simple.

/**
 * @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 don’t know which class will come, can be any class, anyone knows how it would be possible to do so?

  • I’ll give you +1 because I’m an admirer of [tag:symfony]

1 answer

0

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

  • Thank Rodrigo, but that’s not the goal, the skeleton of CRUD I even have, to recover an object by id, can be used the same basic find, however the object is to recover the data sent and convert to the object, as Paramconverter does for example when you send a JSON per post, it can map the object according to the class I specify in the notation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.