Merge branch '2.7' into 2.8
* 2.7: Use the default host even if context is empty and fallback to relative URL if empty host
This commit is contained in:
commit
8af464b888
@ -195,7 +195,7 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
||||
}
|
||||
|
||||
$schemeAuthority = '';
|
||||
if ($host = $this->context->getHost()) {
|
||||
$host = $this->context->getHost();
|
||||
$scheme = $this->context->getScheme();
|
||||
|
||||
if ($requiredSchemes) {
|
||||
@ -241,7 +241,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();
|
||||
@ -252,7 +252,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