Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 62c11e5

Browse filesBrowse files
committed
bug #25491 [Routing] Use the default host even if context is empty (sroze)
This PR was merged into the 2.7 branch. Discussion ---------- [Routing] Use the default host even if context is empty | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25464 | License | MIT | Doc PR | ø When the host in the context is empty... we should still use the default host. **Note:** it seems like a lot of changes but all I did was to remove the `if` and de-indent the code that was inside. Commits ------- 8f357df Use the default host even if context is empty and fallback to relative URL if empty host
2 parents 0f884e0 + 8f357df commit 62c11e5
Copy full SHA for 62c11e5

File tree

2 files changed

+72
-41
lines changed
Filter options

2 files changed

+72
-41
lines changed

‎src/Symfony/Component/Routing/Generator/UrlGenerator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Generator/UrlGenerator.php
+40-41Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -181,63 +181,62 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
181181
}
182182

183183
$schemeAuthority = '';
184-
if ($host = $this->context->getHost()) {
185-
$scheme = $this->context->getScheme();
184+
$host = $this->context->getHost();
185+
$scheme = $this->context->getScheme();
186186

187-
if ($requiredSchemes) {
188-
if (!in_array($scheme, $requiredSchemes, true)) {
189-
$referenceType = self::ABSOLUTE_URL;
190-
$scheme = current($requiredSchemes);
191-
}
192-
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) {
193-
// We do this for BC; to be removed if _scheme is not supported anymore
187+
if ($requiredSchemes) {
188+
if (!in_array($scheme, $requiredSchemes, true)) {
194189
$referenceType = self::ABSOLUTE_URL;
195-
$scheme = $req;
190+
$scheme = current($requiredSchemes);
196191
}
192+
} elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) {
193+
// We do this for BC; to be removed if _scheme is not supported anymore
194+
$referenceType = self::ABSOLUTE_URL;
195+
$scheme = $req;
196+
}
197197

198-
if ($hostTokens) {
199-
$routeHost = '';
200-
foreach ($hostTokens as $token) {
201-
if ('variable' === $token[0]) {
202-
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
203-
$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]]);
204-
205-
if ($this->strictRequirements) {
206-
throw new InvalidParameterException($message);
207-
}
198+
if ($hostTokens) {
199+
$routeHost = '';
200+
foreach ($hostTokens as $token) {
201+
if ('variable' === $token[0]) {
202+
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
203+
$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]]);
208204

209-
if ($this->logger) {
210-
$this->logger->error($message);
211-
}
205+
if ($this->strictRequirements) {
206+
throw new InvalidParameterException($message);
207+
}
212208

213-
return;
209+
if ($this->logger) {
210+
$this->logger->error($message);
214211
}
215212

216-
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
217-
} else {
218-
$routeHost = $token[1].$routeHost;
213+
return;
219214
}
220-
}
221215

222-
if ($routeHost !== $host) {
223-
$host = $routeHost;
224-
if (self::ABSOLUTE_URL !== $referenceType) {
225-
$referenceType = self::NETWORK_PATH;
226-
}
216+
$routeHost = $token[1].$mergedParams[$token[3]].$routeHost;
217+
} else {
218+
$routeHost = $token[1].$routeHost;
227219
}
228220
}
229221

230-
if (self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) {
231-
$port = '';
232-
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
233-
$port = ':'.$this->context->getHttpPort();
234-
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
235-
$port = ':'.$this->context->getHttpsPort();
222+
if ($routeHost !== $host) {
223+
$host = $routeHost;
224+
if (self::ABSOLUTE_URL !== $referenceType) {
225+
$referenceType = self::NETWORK_PATH;
236226
}
227+
}
228+
}
237229

238-
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
239-
$schemeAuthority .= $host.$port;
230+
if ((self::ABSOLUTE_URL === $referenceType || self::NETWORK_PATH === $referenceType) && !empty($host)) {
231+
$port = '';
232+
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
233+
$port = ':'.$this->context->getHttpPort();
234+
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
235+
$port = ':'.$this->context->getHttpsPort();
240236
}
237+
238+
$schemeAuthority = self::NETWORK_PATH === $referenceType ? '//' : "$scheme://";
239+
$schemeAuthority .= $host.$port;
241240
}
242241

243242
if (self::RELATIVE_PATH === $referenceType) {

‎src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,38 @@ public function testHostIsCaseInsensitive()
469469
$this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', array('locale' => 'EN'), UrlGeneratorInterface::NETWORK_PATH));
470470
}
471471

472+
public function testDefaultHostIsUsedWhenContextHostIsEmpty()
473+
{
474+
$routes = $this->getRoutes('test', new Route('/route', array('domain' => 'my.fallback.host'), array('domain' => '.+'), array(), '{domain}', array('http')));
475+
476+
$generator = $this->getGenerator($routes);
477+
$generator->getContext()->setHost('');
478+
479+
$this->assertSame('http://my.fallback.host/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
480+
}
481+
482+
public function testDefaultHostIsUsedWhenContextHostIsEmptyAndSchemeIsNot()
483+
{
484+
$routes = $this->getRoutes('test', new Route('/route', array('domain' => 'my.fallback.host'), array('domain' => '.+'), array(), '{domain}', array('http', 'https')));
485+
486+
$generator = $this->getGenerator($routes);
487+
$generator->getContext()->setHost('');
488+
$generator->getContext()->setScheme('https');
489+
490+
$this->assertSame('https://my.fallback.host/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
491+
}
492+
493+
public function testAbsoluteUrlFallbackToRelativeIfHostIsEmptyAndSchemeIsNot()
494+
{
495+
$routes = $this->getRoutes('test', new Route('/route', array(), array(), array(), '', array('http', 'https')));
496+
497+
$generator = $this->getGenerator($routes);
498+
$generator->getContext()->setHost('');
499+
$generator->getContext()->setScheme('https');
500+
501+
$this->assertSame('/app.php/route', $generator->generate('test', array(), UrlGeneratorInterface::ABSOLUTE_URL));
502+
}
503+
472504
/**
473505
* @group legacy
474506
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.