Skip to content

[Routing][UrlGenerator] set defaultLocale to 'en' instead of null #30405

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
'%7C' => '|',
];

public function __construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = null)
public function __construct(RouteCollection $routes, RequestContext $context, LoggerInterface $logger = null, string $defaultLocale = 'en')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break + it does not really fix the problem if the route does not have an "en" variation.

{
$this->routes = $routes;
$this->context = $context;
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,40 @@ public function testGlobalParameterHasHigherPriorityThanDefault()
$this->assertSame('/app.php/de', $url);
}

public function testGenerateForRouteWithLocale()
{
$routes = $this->getRoutes('test.nl', new Route('/testing/nl'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('_locale', 'nl');
$generator->setContext($context);
$url = $generator->generate('test');

$this->assertSame('/app.php/testing/nl', $url);
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGenerateForRouteWithInvalidLocale()
{
$routes = $this->getRoutes('test.en', new Route('/testing/en'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('_locale', 'nl');
$generator->setContext($context);
$generator->generate('test');
}

public function testGenerateForRouteWithoutDefaultLocale()
{
$routes = $this->getRoutes('test.en', new Route('/testing/en'));
$generator = $this->getGenerator($routes);
$url = $generator->generate('test');

$this->assertSame('/app.php/testing/en', $url);
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
Expand Down