10000 [Routing] adds _fragment special option to url generation for documen… · symfony/symfony@6fc2bd5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6fc2bd5

Browse files
committed
[Routing] adds _fragment special option to url generation for document fragment
1 parent 3805557 commit 6fc2bd5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,24 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
276276
$url = $schemeAuthority.$this->context->getBaseUrl().$url;
277277
}
278278

279-
// add a query string if needed
279+
// extract unused parameters
280280
$extra = array_diff_key($parameters, $variables, $defaults);
281+
282+
// extract fragment
283+
$fragment = isset($extra['_fragment']) ? $extra['_fragment'] : false;
284+
unset($extra['_fragment']);
285+
286+
// add a query string if needed
281287
if ($extra && $query = http_build_query($extra, '', '&')) {
282288
// "/" and "?" can be left decoded for better user experience, see
283289
// http://tools.ietf.org/html/rfc3986#section-3.4
284290
$url .= '?'.strtr($query, array('%2F' => '/'));
285291
}
286292

293+
if ($fragment) {
294+
$url .= '#'.rawurlencode($fragment);
295+
}
296+
287297
return $url;
288298
}
289299

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,14 @@ public function provideRelativePaths()
649649
);
650650
}
651651

652+
public function testFragmentsCanBeAppendedToUrls()
653+
{
654+
$routes = $this->getRoutes('test', new Route('/testing'));
655+
$url = $this->getGenerator($routes)->generate('test', array('_fragment' => 'frag ment'), true);
656+
657+
$this->assertEquals('http://localhost/app.php/testing#frag%20ment', $url);
658+
}
659+
652660
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
653661
{
654662
$context = new RequestContext('/app.php');

0 commit comments

Comments
 (0)
0