8000 feature #16276 Unify URL generator reference type + make linking in p… · symfony/symfony@1705370 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1705370

Browse files
committed
feature #16276 Unify URL generator reference type + make linking in php templates consistent with twig (Tobion)
This PR was merged into the 2.8 branch. Discussion ---------- Unify URL generator reference type + make linking in php templates consistent with twig | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #14672 | License | MIT | Doc PR | TODO Please see ticket. Please merge #16277 first. I will then rebase this to make tests pass. Commits ------- 388fa43 [Templating] deprecate low-level RouterHelper::generate method as it's cumbersome to use constants in templates 97d6828 [Templating] introduce path and url methods in php templates to be in line with twig templates 912fc4d [Routing] deprecate the old url generator reference type values
2 parents a65b489 + 388fa43 commit 1705370

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class Controller extends ContainerAware
3939
/**
4040
* Generates a URL from the given parameters.
4141
*
42-
* @param string $route The name of the route
43-
* @param mixed $parameters An array of parameters
44-
* @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
42+
* @param string $route The name of the route
43+
* @param mixed $parameters An array of parameters
44+
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
4545
*
4646
* @return string The generated URL
4747
*

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/RouterHelper.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,53 @@ public function __construct(UrlGeneratorInterface $router)
3636
/**
3737
* Generates a URL from the given parameters.
3838
*
39-
* @param string $name The name of the route
40-
* @param mixed $parameters An array of parameters
41-
* @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
39+
* @param string $name The name of the route
40+
* @param mixed $parameters An array of parameters
41+
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
4242
*
4343
* @return string The generated URL
4444
*
4545
* @see UrlGeneratorInterface
4646
*/
4747
public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
4848
{
49+
@trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Use the "path" or "url" method instead.', E_USER_DEPRECATED);
50+
4951
return $this->generator->generate($name, $parameters, $referenceType);
5052
}
< 8000 code>5153

54+
/**
55+
* Generates a URL reference (as an absolute or relative path) to the route with the given parameters.
56+
*
57+
* @param string $name The name of the route
58+
* @param mixed $parameters An array of parameters
59+
* @param bool $relative Whether to generate a relative or absolute path
60+
*
61+
* @return string The generated URL reference
62+
*
63+
* @see UrlGeneratorInterface
64+
*/
65+
public function path($name, $parameters = array(), $relative = false)
66+
{
67+
return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
68+
}
69+
70+
/**
71+
* Generates a URL reference (as an absolute URL or network path) to the route with the given parameters.
72+
*
73+
* @param string $name The name of the route
74+
* @param mixed $parameters An array of parameters
75+
* @param bool $schemeRelative Whether to omit the scheme in the generated URL reference
76+
*
77+
* @return string The generated URL reference
78+
*
79+
* @see UrlGeneratorInterface
80+
*/
81+
public function url($name, $parameters = array(), $schemeRelative = false)
82+
{
83+
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
84+
}
85+
5286
/**
5387
* {@inheritdoc}
5488
*/

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
143143
*/
144144
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
145145
67E6 {
146+
if (is_bool($referenceType) || is_string($referenceType)) {
147+
@trigger_error('The hardcoded value you are using for the $referenceType argument of the '.__CLASS__.'::generate method is deprecated since version 2.8 and will not be supported anymore in 3.0. Use the constants defined in the UrlGeneratorInterface instead.', E_USER_DEPRECATED);
148+
149+
if (true === $referenceType) {
150+
$referenceType = self::ABSOLUTE_URL;
151+
} elseif (false === $referenceType) {
152+
$referenceType = self::ABSOLUTE_PATH;
153+
} elseif ('relative' === $referenceType) {
154+
$referenceType = self::RELATIVE_PATH;
155+
} elseif ('network' === $referenceType) {
156+
$referenceType = self::NETWORK_PATH;
157+
}
158+
}
159+
146160
$variables = array_flip($variables);
147161
$mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters);
148162

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ interface UrlGeneratorInterface extends RequestContextAwareInterface
3434
/**
3535
* Generates an absolute URL, e.g. "http://example.com/dir/file".
3636
*/
37-
const ABSOLUTE_URL = true;
37+
const ABSOLUTE_URL = 0;
3838

3939
/**
4040
* Generates an absolute path, e.g. "/dir/file".
4141
*/
42-
const ABSOLUTE_PATH = false;
42+
const ABSOLUTE_PATH = 1;
4343

4444
/**
4545
* Generates a relative path based on the current request path, e.g. "../parent-file".
4646
*
4747
* @see UrlGenerator::getRelativePath()
4848
*/
49-
const RELATIVE_PATH = 'relative';
49+
const RELATIVE_PATH = 2;
5050

5151
/**
5252
* Generates a network path, e.g. "//example.com/dir/file".
5353
* Such reference reuses the current scheme but specifies the host.
5454
*/
55-
const NETWORK_PATH = 'network';
55+
const NETWORK_PATH = 3;
5656

5757
/**
5858
* Generates a URL or path for a specific route based on the given parameters.
@@ -69,9 +69,9 @@ interface UrlGeneratorInterface extends RequestContextAwareInterface
6969
*
7070
* If there is no route with the given name, the generator must throw the RouteNotFoundException.
7171
*
72-
* @param string $name The name of the route
73-
* @param mixed $parameters An array of parameters
74-
* @param bool|string $referenceType The type of reference to be generated (one of the constants)
72+
* @param string $name The name of the route
73+
* @param mixed $parameters An array of parameters
74+
* @param int $referenceType The type of reference to be generated (one of the constants)
7575
*
7676
* @return string The generated URL
7777
*

src/Symfony/Component/Security/Http/Logout/LogoutUrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getLogoutUrl($key = null)
8686
* Generates the logout URL for the firewall.
8787
*
8888
* @param string|null $key The firewall key or null to use the current firewall key
89-
* @param bool|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
89+
* @param int $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
9090
*
9191
* @return string The logout URL
9292
*

0 commit comments

Comments
 (0)
0