/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/RedirectController.php

https://github.com/nattaphat/hgis · PHP · 123 lines · 62 code · 16 blank · 45 comment · 18 complexity · 381d15ddf8ba222be1780add031025ea MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. /**
  16. * Redirects a request to another URL.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class RedirectController extends ContainerAware
  21. {
  22. /**
  23. * Redirects to another route with the given name.
  24. *
  25. * The response status code is 302 if the permanent parameter is false (default),
  26. * and 301 if the redirection is permanent.
  27. *
  28. * In case the route name is empty, the status code will be 404 when permanent is false
  29. * and 410 otherwise.
  30. *
  31. * @param string $route The route name to redirect to
  32. * @param Boolean $permanent Whether the redirection is permanent
  33. *
  34. * @return Response A Response instance
  35. */
  36. public function redirectAction($route, $permanent = false)
  37. {
  38. if ('' == $route) {
  39. return new Response(null, $permanent ? 410 : 404);
  40. }
  41. $attributes = $this->container->get('request')->attributes->get('_route_params');
  42. unset($attributes['route'], $attributes['permanent']);
  43. return new RedirectResponse($this->container->get('router')->generate($route, $attributes, UrlGeneratorInterface::ABSOLUTE_URL), $permanent ? 301 : 302);
  44. }
  45. /**
  46. * Redirects to a URL.
  47. *
  48. * The response status code is 302 if the permanent parameter is false (default),
  49. * and 301 if the redirection is permanent.
  50. *
  51. * In case the path is empty, the status code will be 404 when permanent is false
  52. * and 410 otherwise.
  53. *
  54. * @param string $path The absolute path or URL to redirect to
  55. * @param Boolean $permanent Whether the redirect is permanent or not
  56. * @param string|null $scheme The URL scheme (null to keep the current one)
  57. * @param integer|null $httpPort The HTTP port (null to keep the current one for the same scheme or the configured port in the container)
  58. * @param integer|null $httpsPort The HTTPS port (null to keep the current one for the same scheme or the configured port in the container)
  59. *
  60. * @return Response A Response instance
  61. */
  62. public function urlRedirectAction($path, $permanent = false, $scheme = null, $httpPort = null, $httpsPort = null)
  63. {
  64. if ('' == $path) {
  65. return new Response(null, $permanent ? 410 : 404);
  66. }
  67. $statusCode = $permanent ? 301 : 302;
  68. // redirect if the path is a full URL
  69. if (parse_url($path, PHP_URL_SCHEME)) {
  70. return new RedirectResponse($path, $statusCode);
  71. }
  72. $request = $this->container->get('request');
  73. if (null === $scheme) {
  74. $scheme = $request->getScheme();
  75. }
  76. $qs = $request->getQueryString();
  77. if ($qs) {
  78. $qs = '?'.$qs;
  79. }
  80. $port = '';
  81. if ('http' === $scheme) {
  82. if (null === $httpPort) {
  83. if ('http' === $request->getScheme()) {
  84. $httpPort = $request->getPort();
  85. } elseif ($this->container->hasParameter('request_listener.http_port')) {
  86. $httpPort = $this->container->getParameter('request_listener.http_port');
  87. }
  88. }
  89. if (null !== $httpPort && 80 != $httpPort) {
  90. $port = ":$httpPort";
  91. }
  92. } elseif ('https' === $scheme) {
  93. if (null === $httpsPort) {
  94. if ('https' === $request->getScheme()) {
  95. $httpsPort = $request->getPort();
  96. } elseif ($this->container->hasParameter('request_listener.https_port')) {
  97. $httpsPort = $this->container->getParameter('request_listener.https_port');
  98. }
  99. }
  100. if (null !== $httpsPort && 443 != $httpsPort) {
  101. $port = ":$httpsPort";
  102. }
  103. }
  104. $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;
  105. return new RedirectResponse($url, $statusCode);
  106. }
  107. }