RedirectController.php 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Response;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. /**
  15. * Redirects a request to another URL.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class RedirectController extends ContainerAware
  20. {
  21. /**
  22. * Redirects to another route.
  23. *
  24. * It expects a route path parameter.
  25. * By default, the response status code is 301.
  26. *
  27. * If the route empty, the status code will be 410.
  28. * If the permanent path parameter is set, the status code will be 302.
  29. *
  30. * @param string $route The route pattern to redirect to
  31. * @param Boolean $permanent Whether the redirect is permanent or not
  32. *
  33. * @return Response A Response instance
  34. */
  35. public function redirectAction($route, $permanent = false)
  36. {
  37. if (!$route) {
  38. return new Response(null, 410);
  39. }
  40. $attributes = $this->container->get('request')->attributes->all();
  41. unset($attributes['_route'], $attributes['route'], $attributes['permanent'] );
  42. return new RedirectResponse($this->container->get('router')->generate($route, $attributes), $permanent ? 301 : 302);
  43. }
  44. /**
  45. * Redirects to a URL.
  46. *
  47. * By default, the response status code is 301.
  48. *
  49. * If the path is empty, the status code will be 410.
  50. * If the permanent flag is set, the status code will be 302.
  51. *
  52. * @param string $path The path to redirect to
  53. * @param Boolean $permanent Whether the redirect is permanent or not
  54. * @param Boolean $scheme The URL scheme (null to keep the current one)
  55. * @param integer $httpPort The HTTP port
  56. * @param integer $httpsPort The HTTPS port
  57. *
  58. * @return Response A Response instance
  59. */
  60. public function urlRedirectAction($path, $permanent = false, $scheme = null, $httpPort = 80, $httpsPort = 443)
  61. {
  62. if (!$path) {
  63. return new Response(null, 410);
  64. }
  65. $statusCode = $permanent ? 301 : 302;
  66. // redirect if the path is a full URL
  67. if (parse_url($path, PHP_URL_SCHEME)) {
  68. return new RedirectResponse($path, $statusCode);
  69. }
  70. $request = $this->container->get('request');
  71. if (null === $scheme) {
  72. $scheme = $request->getScheme();
  73. }
  74. $qs = $request->getQueryString();
  75. if ($qs) {
  76. $qs = '?'.$qs;
  77. }
  78. $port = '';
  79. if ('http' === $scheme && 80 != $httpPort) {
  80. $port = ':'.$httpPort;
  81. } elseif ('https' === $scheme && 443 != $httpsPort) {
  82. $port = ':'.$httpsPort;
  83. }
  84. $url = $scheme.'://'.$request->getHost().$port.$request->getBaseUrl().$path.$qs;
  85. return new RedirectResponse($url, $statusCode);
  86. }
  87. }