symfonycrudeasyadmin

Link to some action of a different CRUD controller


Is there any workaround to link a new action, in a CRUD controller made with EasyAdmin 4.x , to an action in another CRUD controller with which it has a OneToMany relation ?

class FirstEntityCrudController extends AbstractCrudController
{
...
public function configureActions(Actions $actions): Actions
{
    return $actions
        ->add(Crud::PAGE_INDEX, Action::new('add-second-entity','Add a second entity')
        ->linkToCrudAction(Action::NEW ???)

            )
        ;
    }
}

The docs say that I can use:

linkToCrudAction(): to execute some method of the current CRUD controller;

But there seems to be no indication on how to "execute some method of a different CRUD controller".

Note: There is a sneaky way around it but it doesn't seem healthy :

   ->linkToUrl('the url to the desired action')
                

Using:


Solution

  • Following @Ruban's comment, and as EasyAdminBundle's docs mention, we can generate a URL for the desired action using the AdminUrlGenerator class as follow:

    class FirstEntityCrudController extends AbstractCrudController
    {
        private $adminUrlGenerator;
    
        public function __construct(AdminUrlGenerator $adminUrlGenerator)
        {
            $this->adminUrlGenerator = $adminUrlGenerator;
        }
    
        // ...
    
        public function configureActions(Actions $actions): Actions
        {
            // The magic happens here 👇
            $url = $this->adminUrlGenerator
                ->setController(SecondEntityCrudController::class)
                ->setAction(Action::NEW)
                ->generateUrl();
    
            return $actions
             // ->... 
                ->add(Crud::PAGE_INDEX, 
                      Action::new('add-second-entity', 'Add second entity')
                    ->linkToUrl($url)
                );
        }
    }
    

    This worked for me.