EasyAdminBundle
EasyAdminBundle copied to clipboard
Redirection before persist and update entity
Hi,
I try to make a redirection before an entity is persisted or updated, but without success. It could be a good thing to allow this. There's maybe a solution, but I don't see how to do it without rewrite the entire method, which is not a good solution.
So, this is what I propose: https://github.com/EasyCorp/EasyAdminBundle/blob/master/src/Controller/AbstractCrudController.php#L286-L290
<?php
// AbstractCrudController.php
// Before
$event = new BeforeEntityPersistedEvent($entityInstance);
$this->get('event_dispatcher')->dispatch($event);
$entityInstance = $event->getEntityInstance();
$this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
// After
$event = new BeforeEntityPersistedEvent($entityInstance);
$this->get('event_dispatcher')->dispatch($event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$entityInstance = $event->getEntityInstance();
$this->persistEntity($this->get('doctrine')->getManagerForClass($context->getEntity()->getFqcn()), $entityInstance);
https://github.com/EasyCorp/EasyAdminBundle/blob/master/src/Event/BeforeEntityPersistedEvent.php
<?php
//Before
namespace EasyCorp\Bundle\EasyAdminBundle\Event;
final class BeforeEntityPersistedEvent
{
private $entityInstance;
public function __construct($entityInstance)
{
$this->entityInstance = $entityInstance;
}
public function getEntityInstance()
{
return $this->entityInstance;
}
}
// After
namespace EasyCorp\Bundle\EasyAdminBundle\Event;
use Symfony\Component\HttpFoundation\Response;
final class BeforeEntityPersistedEvent
{
private $entityInstance;
private $response;
public function __construct($entityInstance)
{
$this->entityInstance = $entityInstance;
}
public function getEntityInstance()
{
return $this->entityInstance;
}
public function getResponse(): Response
{
return $this->response;
}
public function setResponse(Response $response): void
{
$this->response = $response;
}
}
The change would not be very important but would allow more flexibility.
Override the methods of AbstractCrudController new() and edit(), they return a Response object
This is not what I want to do. In my opinion, override a method like new()
or update()
is a very bad solution. This methods are generic, they're not supposed to be overridden for a little need like mine.
For me, events are done for this kind of possibility.
I had a similar problem, I have to redirect after persist. In my case I overrode the new method with this structure
$return = parent::new(
//Catch submit bottom
//...
return $this->redirect();