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: * @package Cake.Routing.Route
13: * @since CakePHP(tm) v 2.0
14: * @license http://www.opensource.org/licenses/mit-license.php MIT License
15: */
16:
17: App::uses('CakeResponse', 'Network');
18: App::uses('CakeRoute', 'Routing/Route');
19:
20: /**
21: * Redirect route will perform an immediate redirect. Redirect routes
22: * are useful when you want to have Routing layer redirects occur in your
23: * application, for when URLs move.
24: *
25: * @package Cake.Routing.Route
26: */
27: class RedirectRoute extends CakeRoute {
28:
29: /**
30: * A CakeResponse object
31: *
32: * @var CakeResponse
33: */
34: public $response = null;
35:
36: /**
37: * The location to redirect to. Either a string or a CakePHP array URL.
38: *
39: * @var mixed
40: */
41: public $redirect;
42:
43: /**
44: * Flag for disabling exit() when this route parses a URL.
45: *
46: * @var bool
47: */
48: public $stop = true;
49:
50: /**
51: * Constructor
52: *
53: * @param string $template Template string with parameter placeholders
54: * @param array $defaults Array of defaults for the route.
55: * @param array $options Array of additional options for the Route
56: */
57: public function __construct($template, $defaults = array(), $options = array()) {
58: parent::__construct($template, $defaults, $options);
59: $this->redirect = (array)$defaults;
60: }
61:
62: /**
63: * Parses a string URL into an array. Parsed URLs will result in an automatic
64: * redirection
65: *
66: * @param string $url The URL to parse
67: * @return bool False on failure
68: */
69: public function parse($url) {
70: $params = parent::parse($url);
71: if (!$params) {
72: return false;
73: }
74: if (!$this->response) {
75: $this->response = new CakeResponse();
76: }
77: $redirect = $this->redirect;
78: if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
79: $redirect = $this->redirect[0];
80: }
81: if (isset($this->options['persist']) && is_array($redirect)) {
82: $redirect += array('named' => $params['named'], 'pass' => $params['pass'], 'url' => array());
83: if (is_array($this->options['persist'])) {
84: foreach ($this->options['persist'] as $elem) {
85: if (isset($params[$elem])) {
86: $redirect[$elem] = $params[$elem];
87: }
88: }
89: }
90: $redirect = Router::reverse($redirect);
91: }
92: $status = 301;
93: if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
94: $status = $this->options['status'];
95: }
96: $this->response->header(array('Location' => Router::url($redirect, true)));
97: $this->response->statusCode($status);
98: $this->response->send();
99: $this->_stop();
100: }
101:
102: /**
103: * There is no reverse routing redirection routes
104: *
105: * @param array $url Array of parameters to convert to a string.
106: * @return mixed either false or a string URL.
107: */
108: public function match($url) {
109: return false;
110: }
111:
112: /**
113: * Stop execution of the current script. Wraps exit() making
114: * testing easier.
115: *
116: * @param int|string $code See http://php.net/exit for values
117: * @return void
118: */
119: protected function _stop($code = 0) {
120: if ($this->stop) {
121: exit($code);
122: }
123: }
124:
125: }
126: