8000 [2.2] [Routing] add support for path-relative URL generation by Tobion · Pull Request #3958 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[2.2] [Routing] add support for path-relative URL generation #3958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* [BC BREAK] restricted the `render` tag to only accept URIs as reference (the signature changed)
* added a render function to render a request
* The `app` global variable is now injected even when using the twig service directly.
* Added an optional parameter to the `path` and `url` function which allows to generate
relative paths (e.g. "../parent-file") and scheme-relative URLs (e.g. "//example.com/dir/file").

2.1.0
-----
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public function getFunctions()
);
}

public function getPath($name, $parameters = array())
public function getPath($name, $parameters = array(), $relative = false)
{
return $this->generator->generate($name, $parameters, false);
return $this->generator->generate($name, $parameters, $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH);
}

public function getUrl($name, $parameters = array())
public function getUrl($name, $parameters = array(), $schemeRelative = false)
{
return $this->generator->generate($name, $parameters, true);
return $this->generator->generate($name, $parameters, $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand All @@ -19,8 +20,8 @@
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Symfony\Component\HttpFoundation\Request;

/**
* Controller is a simple implementation of a Controller.
Expand All @@ -34,15 +35,17 @@ class Controller extends ContainerAware
/**
* Generates a URL from the given parameters.
*
* @param string $route The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean $absolute Whether to generate an absolute URL
* @param string $route The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
*
* @return string The generated URL
*
* @see UrlGeneratorInterface
*/
public function generateUrl($route, $parameters = array(), $absolute = false)
public function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->container->get('router')->generate($route, $parameters, $absolute);
return $this->container->get('router')->generate($route, $parameters, $referenceType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace Symfony\Bundle\FrameworkBundle\Controller;

use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* Redirects a request to another URL.
Expand Down Expand Up @@ -45,7 +46,7 @@ public function redirectAction($route, $permanent = false)
$attributes = $this->container->get('request')->attributes->get('_route_params');
unset($attributes['route'], $attributes['permanent']);

return new RedirectResponse($this->container->get('router')->generate($route, $attributes, true), $permanent ? 301 : 302);
return new RedirectResponse($this->container->get('router')->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $permanent ? 301 : 302);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@ public function __construct(UrlGeneratorInterface $router)
/**
* Generates a URL from the given parameters.
*
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean $absolute Whether to generate an absolute URL
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
*
* @return string The generated URL
*
* @see UrlGeneratorInterface
*/
public function generate($name, $parameters = array(), $absolute = false)
public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
return $this->generator->generate($name, $parameters, $absolute);
return $this->generator->generate($name, $parameters, $referenceType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,40 @@ public function registerListener($key, $logoutPath, $intention, $csrfParameter,
}

/**
* Generate the relative logout URL for the firewall.
* Generates the absolute logout path for the firewall.
*
* @param string $key The firewall key
* @return string The relative logout URL
*
* @return string The logout path
*/
public function getLogoutPath($key)
{
return $this->generateLogoutUrl($key, false);
return $this->generateLogoutUrl($key, UrlGeneratorInterface::ABSOLUTE_PATH);
}

/**
* Generate the absolute logout URL for the firewall.
* Generates the absolute logout URL for the firewall.
*
* @param string $key The firewall key
* @return string The absolute logout URL
*
* @return string The logout URL
*/
public function getLogoutUrl($key)
{
return $this->generateLogoutUrl($key, true);
return $this->generateLogoutUrl($key, UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
* Generate the logout URL for the firewall.
* Generates the logout URL for the firewall.
*
* @param string $key The firewall key
* @param Boolean|string $referenceType The type of reference (one of the constants in UrlGeneratorInterface)
*
* @param string $key The firewall key
* @param Boolean $absolute Whether to generate an absolute URL
* @return string The logout URL
*
* @throws \InvalidArgumentException if no LogoutListener is registered for the key
*/
private function generateLogoutUrl($key, $absolute)
private function generateLogoutUrl($key, $referenceType)
{
if (!array_key_exists($key, $this->listeners)) {
throw new \InvalidArgumentException(sprintf('No LogoutListener found for firewall key "%s".', $key));
Expand All @@ -97,13 +101,13 @@ private function generateLogoutUrl($key, $absolute)
if ('/' === $logoutPath[0]) {
$request = $this->container->get('request');

$url = ($absolute ? $request->getUriForPath($logoutPath) : $request->getBasePath() . $logoutPath);
$url = UrlGeneratorInterface::ABSOLUTE_URL === $referenceType ? $request->getUriForPath($logoutPath) : $request->getBasePath() . $logoutPath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if I pass NETWORK_PATH or RELATIVE_PATH here ? I would still get the behavior of ABSOLUTE_PATH

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a private method that is not used with these params. So no need to handle that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, it is indeed private. I forgot it


if (!empty($parameters)) {
$url .= '?' . http_build_query($parameters);
}
} else {
$url = $this->router->generate($logoutPath, $parameters, $absolute);
$url = $this->router->generate($logoutPath, $parameters, $referenceType);
}

return $url;
Expand Down
10000
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\FormLoginBundle\Security;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

class LocalizedFormFailureHandler implements AuthenticationFailureHandlerInterface
Expand All @@ -28,6 +29,6 @@ public function __construct(RouterInterface $router)

public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
return new RedirectResponse($this->router->generate('localized_login_path', array(), true));
return new RedirectResponse($this->router->generate('localized_login_path', array(), UrlGeneratorInterface::ABSOLUTE_URL));
}
}
6 changes: 6 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ CHANGELOG
are now also allowed.
* [BC BREAK] `RouteCompilerInterface::compile(Route $route)` was made static
(only relevant if you implemented your own RouteCompiler).
* Added possibility to generate relative paths and network paths in the UrlGenerator, e.g.
"../parent-file" and "//example.com/dir/file". The third parameter in
`UrlGeneratorInterface::generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)`
now accepts more values and you should use the constants defined in `UrlGeneratorInterface` for
claritiy. The old method calls with a Boolean parameter will continue to work because they
equal the signature using the constants.

2.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ private function generateDeclaredRoutes()
private function generateGenerateMethod()
{
return <<<EOF
public function generate(\$name, \$parameters = array(), \$absolute = false)
public function generate(\$name, \$parameters = array(), \$referenceType = self::ABSOLUTE_PATH)
{
if (!isset(self::\$declaredRoutes[\$name])) {
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', \$name));
}

list(\$variables, \$defaults, \$requirements, \$tokens, \$hostnameTokens) = self::\$declaredRoutes[\$name];

return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$absolute, \$hostnameTokens);
return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostnameTokens);
}
EOF;
}
Expand Down
Loading
0