Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

php - How to add a dynamic CrudController on a field Symfony/Doctrine

Situation:

I use EasyAdmin for the backend. I have an entity named Vehicle. From it, I've created Car and Bicycle.

I have another entity with a OneToMany relation linked to Vehicle. Problem is, when I click the link created by EasyAdmin, it redirects me to the admin page with the Vehicle crudId. I need it to redirect me to either to the Car or the Bicycle one, depending on the type of the Vehicle.

I've set the AssociationField like this:

public function configureFields(string $pageName): iterable
{
    return [
        AssociationField::new('Vehicle', 'The vehicle')->autocomplete()->setCrudController(Car::class)
    ]
}

Problem:

You can see that I set the crud controller to Car, but here is my problem : I need to set this dynamically, either Car or Bicycle. Is it possible to do it?

Thank you to everyone helping :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So, you can create listener for controller event, and choose the right one and pass it like here https://symfony.com/doc/current/reference/events.html#kernel-controller

use SymfonyComponentHttpKernelEventControllerEvent;

public function onKernelController(ControllerEvent $event)
{
    // ...

    // the controller can be changed to any PHP callable
    $event->setController($myCustomController);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...