[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)
This commit is contained in:
parent
462999d2d2
commit
fc015d5227
@ -185,32 +185,13 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
$url = substr($url, 0, -1) . '%2E';
|
$url = substr($url, 0, -1) . '%2E';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($hostnameTokens) {
|
|
||||||
$host = '';
|
|
||||||
foreach ($hostnameTokens as $token) {
|
|
||||||
if ('variable' === $token[0]) {
|
|
||||||
if (in_array($tparams[$token[3]], array(null, '', false), true)) {
|
|
||||||
// check requirement
|
|
||||||
if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) {
|
|
||||||
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$host = $token[1].$tparams[$token[3]].$host;
|
|
||||||
|
|
||||||
} elseif ('text' === $token[0]) {
|
|
||||||
$host = $token[1].$host;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add a query string if needed
|
// add a query string if needed
|
||||||
$extra = array_diff_key($parameters, $variables);
|
$extra = array_diff_key($parameters, $variables);
|
||||||
if ($extra && $query = http_build_query($extra, '', '&')) {
|
if ($extra && $query = http_build_query($extra, '', '&')) {
|
||||||
$url .= '?'.$query;
|
$url .= '?'.$query;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->context->getHost()) {
|
if ($host = $this->context->getHost()) {
|
||||||
$scheme = $this->context->getScheme();
|
$scheme = $this->context->getScheme();
|
||||||
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
|
if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme != $req) {
|
||||||
$absolute = true;
|
$absolute = true;
|
||||||
@ -218,15 +199,29 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($hostnameTokens) {
|
if ($hostnameTokens) {
|
||||||
|
$ghost = '';
|
||||||
|
foreach ($hostnameTokens as $token) {
|
||||||
|
if ('variable' === $token[0]) {
|
||||||
|
if (in_array($mergedParams[$token[3]], array(null, '', false), true)) {
|
||||||
|
// check requirement
|
||||||
|
if ($mergedParams[$token[3]] && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
|
||||||
|
throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $mergedParams[$token[3]]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$ghost = $token[1].$mergedParams[$token[3]].$ghost;
|
||||||
|
} elseif ('text' === $token[0]) {
|
||||||
|
$ghost = $token[1].$ghost;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($ghost != $host) {
|
||||||
|
$host = $ghost;
|
||||||
$absolute = true;
|
$absolute = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($absolute) {
|
if ($absolute) {
|
||||||
|
|
||||||
if (!$hostnameTokens) {
|
|
||||||
$host = $this->context->getHost();
|
|
||||||
}
|
|
||||||
|
|
||||||
$port = '';
|
$port = '';
|
||||||
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
|
||||||
$port = ':'.$this->context->getHttpPort();
|
$port = ':'.$this->context->getHttpPort();
|
||||||
|
@ -377,6 +377,27 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html'));
|
$this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWithHostnameDifferentFromContext()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
|
||||||
|
|
||||||
|
$this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' =>'Fabien', 'locale' => 'fr')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWithHostnameSameAsContext()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
|
||||||
|
|
||||||
|
$this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' =>'Fabien', 'locale' => 'fr')));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWithHostnameSameAsContextAndAbsolute()
|
||||||
|
{
|
||||||
|
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
|
||||||
|
|
||||||
|
$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));
|
||||||
|
}
|
||||||
|
|
||||||
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
|
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
|
||||||
{
|
{
|
||||||
$context = new RequestContext('/app.php');
|
$context = new RequestContext('/app.php');
|
||||||
|
Reference in New Issue
Block a user