Skip to content

Commit

Permalink
bug #3361 Fixed Dashboard Crud links when using prefixed routes (jbtr…
Browse files Browse the repository at this point in the history
…onics)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Fixed Dashboard Crud links when using prefixed routes

Symfony allows to add prefixes to all imported routes, to choose between different languages easily (see https://symfony.com/doc/current/routing.html#localized-routes-i18n).

EasyAdmin 3 had problems when using prefixed routes, because the routes generated for CRUD controllers did not matched the locale the user has selected by the route prefix. Instead, always the last defined route prefix was used.

The reason for this that the controller name (which was used as key by CacheWarmer) is the same for all the different prefixed routes. I changed this behavior and added some logic to determine the right route based on Request object.

Now EasyAdmin should work fine with locale prefixed routes.

Commits
-------

dfce1e8 Fixed DashboardRoute determination when using locale prefixed routes.
  • Loading branch information
javiereguiluz committed Jun 23, 2020
2 parents fb1ec2b + dfce1e8 commit 3cbc0a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/Cache/CacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function warmUp($cacheDirectory)
continue;
}

$dashboardRoutes[$controller->toString()] = $routeName;
/* Controller names are not unique when locale prefixes are defined, therefore use the unique routename
(which is suffixed with .locale) as key, to allow us later to choose the correct localized route */
$dashboardRoutes[$routeName] = $controller->toString();
}

(new Filesystem())->dumpFile(
Expand Down
14 changes: 12 additions & 2 deletions src/Factory/AdminContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,20 @@ private function getDashboardDto(Request $request, DashboardControllerInterface
$dashboardControllerRoutes = require $this->cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
$dashboardController = \get_class($dashboardControllerInstance).'::index';
$dashboardRouteName = null;
foreach ($dashboardControllerRoutes as $controller => $routeName) {

foreach ($dashboardControllerRoutes as $routeName => $controller) {
if ($controller === $dashboardController) {
$dashboardRouteName = $routeName;
break;

//Try to get locale part of the route (e.g. en in 'admin_route.en')
$locale = explode('.', $dashboardRouteName)[1] ?? null;

//If no locale prefix are generated or we found our matching locale we can skip the other iterations
if ($locale === null || $locale === $request->getLocale()) {
break;
} else { //Otherwise we have to check if we find a better match later
continue;
}
}
}

Expand Down

0 comments on commit 3cbc0a4

Please sign in to comment.