Search code examples
symfonyeasyadmin

EasyAdmin : redirect to the page of Crud controller with custom actions


I have a MessageCrudController and I made a custom action with easy admin to import some messages templates with an API

Now I want to be redirected to the list of my messages in my dashboard, but I don't know how to do that, It's my first custom action

there is page where I want to be redirected :

enter image description here

there is what I tried :

MessageCrudController

public function fetchMessages()
    {
      some logic to get my messages...
      return $this->redirect($this->generateUrl('admin', [
                'action' => 'index',
                'entity' => 'MessageCrudController',
            ]));
    }

Actually, I tried this return, and it redirect me to the homepage of my dashboard, and not to the messages page

someone have an Idea to how can I be redirected to my message page of my dashboard? thanks


Solution

  • The 'entity' key seems to be wrong for url generator properties.

    I prefer to get AdminUrlGenerator service from the container and generate urls using it:

    $this->container->get(AdminUrlGenerator::class)
        ->setController(MessageCrudController::class)
        ->setAction(Action::INDEX)
        ->generateUrl();
    

    This way much more easy to avoid mistakes here.