RedirectRoute.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Routing\Route;
  16. use Cake\Network\Response;
  17. use Cake\Routing\Router;
  18. use Cake\Routing\Route\Route;
  19. /**
  20. * Redirect route will perform an immediate redirect. Redirect routes
  21. * are useful when you want to have Routing layer redirects occur in your
  22. * application, for when URLs move.
  23. *
  24. */
  25. class RedirectRoute extends Route {
  26. /**
  27. * A Response object
  28. *
  29. * @var \Cake\Network\Response
  30. */
  31. public $response = null;
  32. /**
  33. * The location to redirect to. Either a string or a CakePHP array URL.
  34. *
  35. * @var mixed
  36. */
  37. public $redirect;
  38. /**
  39. * Constructor
  40. *
  41. * @param string $template Template string with parameter placeholders
  42. * @param array|string $defaults Defaults for the route.
  43. * @param array $options Array of additional options for the Route
  44. */
  45. public function __construct($template, $defaults = [], array $options = []) {
  46. parent::__construct($template, $defaults, $options);
  47. if (is_array($defaults) && isset($defaults['redirect'])) {
  48. $defaults = $defaults['redirect'];
  49. }
  50. $this->redirect = (array)$defaults;
  51. }
  52. /**
  53. * Parses a string URL into an array. Parsed URLs will result in an automatic
  54. * redirection
  55. *
  56. * @param string $url The URL to parse
  57. * @return bool False on failure
  58. */
  59. public function parse($url) {
  60. $params = parent::parse($url);
  61. if (!$params) {
  62. return false;
  63. }
  64. if (!$this->response) {
  65. $this->response = new Response();
  66. }
  67. $redirect = $this->redirect;
  68. if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
  69. $redirect = $this->redirect[0];
  70. }
  71. if (isset($this->options['persist']) && is_array($redirect)) {
  72. $redirect += array('pass' => $params['pass'], 'url' => array());
  73. if (is_array($this->options['persist'])) {
  74. foreach ($this->options['persist'] as $elem) {
  75. if (isset($params[$elem])) {
  76. $redirect[$elem] = $params[$elem];
  77. }
  78. }
  79. }
  80. $redirect = Router::reverse($redirect);
  81. }
  82. $status = 301;
  83. if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
  84. $status = $this->options['status'];
  85. }
  86. $this->response->header(array(
  87. 'Location' => Router::url($redirect, true)
  88. ));
  89. $this->response->statusCode($status);
  90. $this->response->send();
  91. $this->response->stop();
  92. }
  93. /**
  94. * There is no reverse routing redirection routes
  95. *
  96. * @param array $url Array of parameters to convert to a string.
  97. * @param array $context Array of request context parameters.
  98. * @return mixed either false or a string url.
  99. */
  100. public function match(array $url, array $context = array()) {
  101. return false;
  102. }
  103. }