EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

DashboardController default route with locale

Open Axel29 opened this issue 9 months ago • 7 comments

Describe the bug

When using the new AdminDashboard PHP Attribute to set the default route with a locale set, after logging in, the locale is not set in the URL, giving a 404.

To Reproduce

  1. Create a DashboardController
  2. Add the following PHP Attribute #[AdminDashboard('/admin/{_locale}', 'admin')]
  3. Remove the var/cache folder's content and clear Symfony's cache using php bin/console cache:clear

Additional context Setting the route at the index method level works:

    #[IsGranted('ROLE_ADMIN')]
    #[Route('/admin/{_locale}', name: 'admin')]
    public function index(string $_locale = 'fr'): Response
    {
        $adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);

        return $this->redirect($adminUrlGenerator->setController(ShopifyOrderCrudController::class)->generateUrl());
    }

Axel29 avatar Mar 18 '25 11:03 Axel29

It is not an EA issue. From the Symfony Routing docs:

If you want to always include some default value in the generated URL (for example to force the generation of /blog/1 instead of /blog in the previous example) add the ! character before the parameter name: /blog/{!page}

fracsi avatar Mar 18 '25 14:03 fracsi

It is not an EA issue. From the Symfony Routing docs:

If you want to always include some default value in the generated URL (for example to force the generation of /blog/1 instead of /blog in the previous example) add the ! character before the parameter name: /blog/{!page}

Hi, thanks for the answer. But EasyAdmin's documentation also says "If you are implementing a multilingual dashboard, add the _locale parameter to the route (e.g. /admin/{_locale}).".

Thus, what is the correct way to add the locale to the admin since after logging in, I get redirected without the locale parameter?

I tried your solution by using this syntax, but it didn't work either:

<?php

declare(strict_types=1);

namespace App\Controller\Admin;

#[AdminDashboard(routePath: '/admin/{!_locale}', routeName: 'admin')]
class DashboardController extends AbstractDashboardController
{
    ...

    #[IsGranted('ROLE_ADMIN')]
    public function index(string $_locale = 'fr'): Response
    {
        $adminUrlGenerator = $this->container->get(AdminUrlGenerator::class);

        return $this->redirect($adminUrlGenerator->setController(ShopifyOrderCrudController::class)->generateUrl());
    }

    ...
}

Axel29 avatar Mar 19 '25 13:03 Axel29

You can inline the default value with {parameter_name?default_value} E.g.:

#[AdminDashboard(routePath: '/admin/{_locale?fr}')]

fracsi avatar Mar 19 '25 13:03 fracsi

You can inline the default value with {parameter_name?default_value} E.g.:

#[AdminDashboard(routePath: '/admin/{_locale?fr}')]

Tried that too and still doesn't work. My guess is this is because we're using the "AdminDashboard" PHP Attribute, not the Symfony "Route" PHP Attribute.

Axel29 avatar Mar 19 '25 13:03 Axel29

Tried that too and still doesn't work. My guess is this is because we're using the "AdminDashboard" PHP Attribute, not the Symfony "Route" PHP Attribute.

I'm guessing you're right. You could override the redirect after login - successhandler, custom authenticator, target_path parameter

fracsi avatar Mar 19 '25 13:03 fracsi

@Axel29 I just tested it locally with and it works with:

// Note the ! in the parameter
#[AdminDashboard(routePath: '/admin/{!_locale?fr}', routeName: 'admin')]

And default_target_path is the route name: admin on firewall.

fracsi avatar Mar 19 '25 14:03 fracsi

@Axel29 Could you please help test if the below patch fixes your issue? https://github.com/EasyCorp/EasyAdminBundle/pull/6954

andersonamuller avatar May 22 '25 22:05 andersonamuller