Skip to content

Commit

Permalink
bug #6754 Add default locale to all pretty routes (danut007ro)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Add default locale to all pretty routes

This PR is a fix for an issue I'm having in tests with a multilingual app using pretty urls. The issue does not occur when using old urls. My issue is that I had the following error when generating urls in tests using `CrudTestUrlGeneration` trait

```
Some mandatory parameters are missing ("_locale") to generate a URL for route "admin_..."
```

This exception is thrown in [UrlGenerator](https://github.com/symfony/symfony/blob/7.2/src/Symfony/Component/Routing/Generator/UrlGenerator.php#L150) when no locale is found in route defaults, request context or parameters.

When NOT using pretty urls, the `_locale` is present in [route defaults](https://github.com/symfony/symfony/blob/7.2/src/Symfony/Component/Routing/Generator/CompiledUrlGenerator.php#L55), but when using pretty urls there is no locale.

The PR adds the default `_locale` for all routes generated by EasyAdmin.

Commits
-------

7ae7911 Add default locale to all pretty routes
  • Loading branch information
javiereguiluz committed Feb 3, 2025
2 parents 2537a4c + 7ae7911 commit 5e48501
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
->arg(2, service('cache.easyadmin'))
->arg(3, service('filesystem'))
->arg(4, '%kernel.build_dir%')
->arg(5, '%kernel.default_locale%')

->set(AdminRouteLoader::class)
->arg(0, service(AdminRouteGenerator::class))
Expand Down
2 changes: 2 additions & 0 deletions src/Router/AdminRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function __construct(
private CacheItemPoolInterface $cache,
private Filesystem $filesystem,
private string $buildDir,
private string $defaultLocale,
) {
}

Expand Down Expand Up @@ -165,6 +166,7 @@ private function generateAdminRoutes(): array
}

$defaults = [
'_locale' => $this->defaultLocale,
'_controller' => $crudControllerFqcn.'::'.$actionName,
EA::ROUTE_CREATED_BY_EASYADMIN => true,
EA::DASHBOARD_CONTROLLER_FQCN => $dashboardFqcn,
Expand Down
1 change: 1 addition & 0 deletions tests/Router/AdminRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testFindRoute(?string $dashboardControllerFqcn, ?string $crudCon
$cacheMock,
new Filesystem(),
$client->getKernel()->getBuildDir(),
'en',
);

$routeName = $adminRouteGenerator->findRouteName($dashboardControllerFqcn, $crudControllerFqcn, $action);
Expand Down

0 comments on commit 5e48501

Please sign in to comment.