1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4: * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * Redistributions of files must retain the above copyright notice.
8: *
9: * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
10: * @link http://cakephp.org CakePHP(tm) Project
11: * @package Cake.Routing.Route
12: * @since CakePHP(tm) v 2.0
13: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
14: */
15:
16: App::uses('CakeResponse', 'Network');
17: App::uses('CakeRoute', 'Routing/Route');
18:
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: * @package Cake.Routing.Route
25: */
26: class RedirectRoute extends CakeRoute {
27:
28: /**
29: * A CakeResponse object
30: *
31: * @var CakeResponse
32: */
33: public $response = null;
34:
35: /**
36: * The location to redirect to. Either a string or a cake array url.
37: *
38: * @var mixed
39: */
40: public $redirect;
41:
42: /**
43: * Flag for disabling exit() when this route parses a url.
44: *
45: * @var boolean
46: */
47: public $stop = true;
48:
49: /**
50: * Constructor
51: *
52: * @param string $template Template string with parameter placeholders
53: * @param array $defaults Array of defaults for the route.
54: * @param array $options Array of additional options for the Route
55: */
56: public function __construct($template, $defaults = array(), $options = array()) {
57: parent::__construct($template, $defaults, $options);
58: $this->redirect = (array)$defaults;
59: }
60:
61: /**
62: * Parses a string url into an array. Parsed urls will result in an automatic
63: * redirection
64: *
65: * @param string $url The url to parse
66: * @return boolean False on failure
67: */
68: public function parse($url) {
69: $params = parent::parse($url);
70: if (!$params) {
71: return false;
72: }
73: if (!$this->response) {
74: $this->response = new CakeResponse();
75: }
76: $redirect = $this->redirect;
77: if (count($this->redirect) == 1 && !isset($this->redirect['controller'])) {
78: $redirect = $this->redirect[0];
79: }
80: if (isset($this->options['persist']) && is_array($redirect)) {
81: $redirect += array('named' => $params['named'], 'pass' => $params['pass'], 'url' => array());
82: $redirect = Router::reverse($redirect);
83: }
84: $status = 301;
85: if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
86: $status = $this->options['status'];
87: }
88: $this->response->header(array('Location' => Router::url($redirect, true)));
89: $this->response->statusCode($status);
90: $this->response->send();
91: $this->_stop();
92: }
93:
94: /**
95: * There is no reverse routing redirection routes
96: *
97: * @param array $url Array of parameters to convert to a string.
98: * @return mixed either false or a string url.
99: */
100: public function match($url) {
101: return false;
102: }
103:
104: /**
105: * Stop execution of the current script. Wraps exit() making
106: * testing easier.
107: *
108: * @param integer|string $status see http://php.net/exit for values
109: * @return void
110: */
111: protected function _stop($code = 0) {
112: if ($this->stop) {
113: exit($code);
114: }
115: }
116:
117: }
118: