Use the default host even if context is empty and fallback to relative URL if empty host
This commit is contained in:
parent
8e54591f24
commit
8f357df75b
@ -181,7 +181,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
||||
}
|
||||
|
||||
$schemeAuthority = '';
|
||||
if ($host = $this->context->getHost()) {
|
||||
$host = $this->context->getHost();
|
||||
$scheme = $this->context->getScheme();
|
||||
|
||||
if ($requiredSchemes) {
|
||||
@ -227,7 +227,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
||||
}
|
||||
}
|
||||
|
||||
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
|
||||
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
|
||||
$port = '';
|
||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
||||
$port = ':'.$this->context->getHttpPort();
|
||||
@ -238,7 +238,6 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
||||
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
|
||||
$schemeAuthority .= $host.$port;
|
||||
}
|
||||
}
|
||||
|
||||
if (self::RELATIVE_PATH === $referenceType) {
|
||||
$url = self::getRelativePath($this->context->getPathInfo(), $url);
|
||||
|
@ -469,6 +469,38 @@ class UrlGeneratorTest extends TestCase
|
||||
$this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', array('locale' => 'EN'), UrlGeneratorInterface::NETWORK_PATH));
|
||||
}
|
||||
|
||||
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
|
||||
{
|
||||
$routes = $this->getRoutes('test', new Route('/route', array('domain' => 'my.fallback.host'), array('domain' => '.+'), array(), '{domain}', array('http')));
|
||||
|
||||
$generator = $this->getGenerator($routes);
|
||||
$generator->getContext()->setHost('');
|
||||
|
||||
$this->assertSame('http://my.fallback.host/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
|
||||
}
|
||||
|
||||
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndSchemeIsNot()
|
||||
{
|
||||
$routes = $this->getRoutes('test', new Route('/route', array('domain' => 'my.fallback.host'), array('domain' => '.+'), array(), '{domain}', array('http', 'https')));
|
||||
|
||||
$generator = $this->getGenerator($routes);
|
||||
$generator->getContext()->setHost('');
|
||||
$generator->getContext()->setScheme('https');
|
||||
|
||||
$this->assertSame('https://my.fallback.host/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
|
||||
}
|
||||
|
||||
public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
|
||||
{
|
||||
$routes = $this->getRoutes('test', new Route('/route', array(), array(), array(), '', array('http', 'https')));
|
||||
|
||||
$generator = $this->getGenerator($routes);
|
||||
$generator->getContext()->setHost('');
|
||||
$generator->getContext()->setScheme('https');
|
||||
|
||||
$this->assertSame('/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
|
||||
}
|
||||
|
||||
/**
|
||||
* @group legacy
|
||||
*/
|
||||
|
Reference in New Issue
Block a user