Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fc015d5

Browse files
fabpotarnaud-lb
authored andcommitted
[Routing] fixed route generation with a hostname pattern when the hostname is the same as the current one (no need to force the generated URL to be absolute)
1 parent 462999d commit fc015d5

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

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

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,48 +185,43 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
185185
$url = substr($url, 0, -1) . '%2E';
186186
}
187187

188-
if ($hostnameTokens) {
189-
$host = '';
190-
foreach ($hostnameTokens as $token) {
191-
if ('variable' === $token[0]) {
192-
if (in_array($tparams[$token[3]], array(null, '', false), true)) {
193-
// check requirement
194-
if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
195-
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
196-
}
197-
}
198-
199-
$host = $token[1].$tparams[$token[3]].$host;
200-
201-
} elseif ('text' === $token[0]) {
202-
$host = $token[1].$host;
203-
}
204-
}
205-
}
206-
207188
// add a query string if needed
208189
$extra = array_diff_key($parameters, $variables);
209190
if ($extra && $query = http_build_query($extra, '', '&')) {
210191
$url .= '?'.$query;
211192
}
212193

213-
if ($this->context->getHost()) {
194+
if ($host = $this->context->getHost()) {
214195
$scheme = $this->context->getScheme();
215196
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
216197
$absolute = true;
217198
$scheme = $req;
218199
}
219200

220201
if ($hostnameTokens) {
221-
$absolute = true;
222-
}
202+
$ghost = '';
203+
foreach ($hostnameTokens as $token) {
204+
if ('variable' === $token[0]) {
205+
if (in_array($mergedParams[$token[3]], array(null, '', false), true)) {
206+
// check requirement
207+
if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
208+
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]));
209+
}
210+
}
223211

224-
if ($absolute) {
212+
$ghost = $token[1].$mergedParams[$token[3]].$ghost;
213+
} elseif ('text' === $token[0]) {
214+
$ghost = $token[1].$ghost;
215+
}
216+
}
225217

226-
if (!$hostnameTokens) {
227-
$host = $this->context->getHost();
218+
if ($ghost != $host) {
219+
$host = $ghost;
220+
$absolute = true;
228221
}
222+
}
229223

224+
if ($absolute) {
230225
$port = '';
231226
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
232227
$port = ':'.$this->context->getHttpPort();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,27 @@ public function testDefaultRequirementOfVariableDisallowsNextSeparator()
377377
$this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html'));
378378
}
379379

380+
public function testWithHostnameDifferentFromContext()
381+
{
382+
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
383+
384+
$this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' =>'Fabien', 'locale' => 'fr')));
385+
}
386+
387+
public function testWithHostnameSameAsContext()
388+
{
389+
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
390+
391+
$this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr')));
392+
}
393+
394+
public function testWithHostnameSameAsContextAndAbsolute()
395+
{
396+
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
397+
398+
$this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr'), true));
399+
}
400+
380401
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
381402
{
382403
$context = new RequestContext('/app.php');

0 commit comments

Comments
 (0)