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

Skip to content

Commit 8e5ec77

Browse files
committed
[Router] added appending of new optional document fragment
added a new optional parameter to the generate method for the document fragment. when specified this is appended to generated urls.
1 parent 3805557 commit 8e5ec77

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ private function generateDeclaredRoutes()
109109
private function generateGenerateMethod()
110110
{
111111
return <<<EOF
112-
public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH)
112+
public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH, \$fragment = null)
113113
{
114114
if (!isset(self::\$declaredRoutes[\$name])) {
115115
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', \$name));
116116
}
117117
118118
list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens, \$requiredSchemes) = self::\$declaredRoutes[\$name];
119119
120-
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens, \$requiredSchemes);
120+
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens, \$requiredSchemes, \$fragment);
121121
}
122122
EOF;
123123
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function isStrictRequirements()
128128
/**
129129
* {@inheritdoc}
130130
*/
131-
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
131+
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH, $fragment = null)
132132
{
133133
if (null === $route = $this->routes->get($name)) {
134134
throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name));
@@ -137,15 +137,15 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
137137
// the Route has a cache of its own and is not recompiled as long as it does not get modified
138138
$compiledRoute = $route->compile();
139139

140-
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens(), $route->getSchemes());
140+
return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens(), $route->getSchemes(), $fragment);
141141
}
142142

143143
/**
144144
* @throws MissingMandatoryParametersException When some parameters are missing that are mandatory for the route
145145
* @throws InvalidParameterException When a parameter value for a placeholder is not correct because
146146
* it does not match the requirement
147147
*/
148-
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
148+
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array(), $fragment)
149149
{
150150
$variables = array_flip($variables);
151151
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters);
@@ -284,6 +284,11 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa
284284
$url .= '?'.strtr($query, array('%2F' => '/'));
285285
}
286286

287+
// add fragment if needed
288+
if ($fragment) {
289+
$url .= '#' . rawurlencode($fragment);
290+
}
291+
287292
return $url;
288293
}
289294

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ interface UrlGeneratorInterface extends RequestContextAwareInterface
7373
* @param string $name The name of the route
7474
* @param mixed $parameters An array of parameters
7575
* @param bool|string $referenceType The type of reference to be generated (one of the constants)
76+
* @param string $fragment The document fragment
7677
*
7778
* @return string The generated URL
7879
*
@@ -83,5 +84,5 @@ interface UrlGeneratorInterface extends RequestContextAwareInterface
8384
*
8485
* @api
8586
*/
86-
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH);
87+
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH, $fragment = null);
8788
}

src/Symfony/Component/Routing/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ public function getContext()
212212
/**
213213
* {@inheritdoc}
214214
*/
215-
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
215+
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH, $fragment = null)
216216
{
217-
return $this->getGenerator()->generate($name, $parameters, $referenceType);
217+
return $this->getGenerator()->generate($name, $parameters, $referenceType, $fragment);
218218
}
219219

220220
/**

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(), true, 'frag ment');
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)