Jean-marc Strauven
Jean-marc Strauven

Reputation: 43

EasyAdmin 3 - Generate URL for redirect in controller (no crud)

I have made a controller (not a crud) with this extension :

class ScanController extends AbstractDashboardController

In this controller, I have a process with more steps and for each step I have create a action :

    /**
     * @Route("/scan/step1", name="scan_step1")
     */
    public function step1(Request $request, EntityManagerInterface $entityManager): Response
    {
}

    /**
     * @Route("/scan/step2/{id_scan}", name="scan_step2")
     */
    public function step2(int $id_scan, Request $request, EntityManagerInterface $entityManager): Response
    {
}

In my dashboard menu config I have add a menu to the first step :

MenuItem::linktoRoute('Scan', 'fa fa-barcode', 'scan_step1'),

The url of the Step1 is :

https://xxx/admin?menuIndex=2&routeName=scan_step1&signature=WeCEAS5-LhXL1Zy50HTVPuFjUpDKc7K0vdBLUY-T45E&submenuIndex=1

And this is ok but now, when I have done in the step1, I want to redirect the customer to the Step2 and I have used the simple "redirectToRoute" function :

    return $this->redirectToRoute('scan_step2', [
        'id_scan'=>$scan->getId(),
    ]);

But when the page is open, I don't have any menu any more....I'm in the template but "outside" the easyadmin "world" and the URL is now :

https://xxxx/scan/step2/14

I'm sure that I need to generate by redirect URL with a easyadmin function but I dont find the way to make this :-( Is it the AdminUrlGenerator and something else and how ?

Upvotes: 1

Views: 2914

Answers (2)

chris_cm
chris_cm

Reputation: 167

Here is the solution for EasyAdmin 4:

Inject in your controller AdminUrlGenerator from EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator

AdminUrlGenerator $adminUrlGenerator

and then generate the URL for the route name:

$url = $adminUrlGenerator->setRoute('my_route')->generateUrl()

which you can then use for a redirect:

return $this->redirect($url);

Upvotes: 0

Jean-marc Strauven
Jean-marc Strauven

Reputation: 43

I have find the solution. First I extend with EasyAdminController :

class ScanController extends EasyAdminController
{  
}

and for the redirection :

    $url = $this->adminUrlGenerator
    ->setRoute('scan_step2',[
        'id_scan'=>$scan->getId(),
    ])
    ->generateUrl();

    return $this->redirect($url);

And Everything is fine now.

Upvotes: 0

Related Questions