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,65 +181,64 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
}
|
}
|
||||||
|
|
||||||
$schemeAuthority = '';
|
$schemeAuthority = '';
|
||||||
if ($host = $this->context->getHost()) {
|
$host = $this->context->getHost();
|
||||||
$scheme = $this->context->getScheme();
|
$scheme = $this->context->getScheme();
|
||||||
|
|
||||||
if ($requiredSchemes) {
|
if ($requiredSchemes) {
|
||||||
if (!in_array($scheme, $requiredSchemes, true)) {
|
if (!in_array($scheme, $requiredSchemes, true)) {
|
||||||
$referenceType = self::ABSOLUTE_URL;
|
|
||||||
$scheme = current($requiredSchemes);
|
|
||||||
}
|
|
||||||
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) {
|
|
||||||
// We do this for BC; to be removed if _scheme is not supported anymore
|
|
||||||
$referenceType = self::ABSOLUTE_URL;
|
$referenceType = self::ABSOLUTE_URL;
|
||||||
$scheme = $req;
|
$scheme = current($requiredSchemes);
|
||||||
}
|
}
|
||||||
|
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) {
|
||||||
|
// We do this for BC; to be removed if _scheme is not supported anymore
|
||||||
|
$referenceType = self::ABSOLUTE_URL;
|
||||||
|
$scheme = $req;
|
||||||
|
}
|
||||||
|
|
||||||
if ($hostTokens) {
|
if ($hostTokens) {
|
||||||
$routeHost = '';
|
$routeHost = '';
|
||||||
foreach ($hostTokens as $token) {
|
foreach ($hostTokens as $token) {
|
||||||
if ('variable' === $token[0]) {
|
if ('variable' === $token[0]) {
|
||||||
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
|
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
|
||||||
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
|
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
|
||||||
|
|
||||||
if ($this->strictRequirements) {
|
if ($this->strictRequirements) {
|
||||||
throw new InvalidParameterException($message);
|
throw new InvalidParameterException($message);
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->logger) {
|
|
||||||
$this->logger->error($message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
|
if ($this->logger) {
|
||||||
} else {
|
$this->logger->error($message);
|
||||||
$routeHost = $token[1].$routeHost;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($routeHost !== $host) {
|
return;
|
||||||
$host = $routeHost;
|
|
||||||
if (self::ABSOLUTE_URL !== $referenceType) {
|
|
||||||
$referenceType = self::NETWORK_PATH;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
|
||||||
|
} else {
|
||||||
|
$routeHost = $token[1].$routeHost;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
|
if ($routeHost !== $host) {
|
||||||
$port = '';
|
$host = $routeHost;
|
||||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
if (self::ABSOLUTE_URL !== $referenceType) {
|
||||||
$port = ':'.$this->context->getHttpPort();
|
$referenceType = self::NETWORK_PATH;
|
||||||
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
|
|
||||||
$port = ':'.$this->context->getHttpsPort();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
|
|
||||||
$schemeAuthority .= $host.$port;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
|
||||||
|
$port = '';
|
||||||
|
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
||||||
|
$port = ':'.$this->context->getHttpPort();
|
||||||
|
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
|
||||||
|
$port = ':'.$this->context->getHttpsPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
|
||||||
|
$schemeAuthority .= $host.$port;
|
||||||
|
}
|
||||||
|
|
||||||
if (self::RELATIVE_PATH === $referenceType) {
|
if (self::RELATIVE_PATH === $referenceType) {
|
||||||
$url = self::getRelativePath($this->context->getPathInfo(), $url);
|
$url = self::getRelativePath($this->context->getPathInfo(), $url);
|
||||||
} else {
|
} else {
|
||||||
|
@ -469,6 +469,38 @@ class UrlGeneratorTest extends TestCase
|
|||||||
$this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', array('locale' => 'EN'), UrlGeneratorInterface::NETWORK_PATH));
|
$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
|
* @group legacy
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user