DashboardController default route with locale
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
- Create a DashboardController
- Add the following PHP Attribute
#[AdminDashboard('/admin/{_locale}', 'admin')] - Remove the
var/cachefolder's content and clear Symfony's cache usingphp 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());
}
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}
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());
}
...
}
You can inline the default value with {parameter_name?default_value}
E.g.:
#[AdminDashboard(routePath: '/admin/{_locale?fr}')]
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.
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
@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.
@Axel29 Could you please help test if the below patch fixes your issue? https://github.com/EasyCorp/EasyAdminBundle/pull/6954