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:
17: use Cake\Network\Response;
18: use Cake\Routing\Router;
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: */
26: class RedirectRoute extends Route
27: {
28:
29: /**
30: * A Response object
31: *
32: * @var \Cake\Network\Response
33: */
34: public $response = null;
35:
36: /**
37: * The location to redirect to. Either a string or a CakePHP array URL.
38: *
39: * @var array|string
40: */
41: public $redirect;
42:
43: /**
44: * Constructor
45: *
46: * @param string $template Template string with parameter placeholders
47: * @param array|string $defaults Defaults for the route.
48: * @param array $options Array of additional options for the Route
49: */
50: public function __construct($template, $defaults = [], array $options = [])
51: {
52: parent::__construct($template, $defaults, $options);
53: if (is_array($defaults) && isset($defaults['redirect'])) {
54: $defaults = $defaults['redirect'];
55: }
56: $this->redirect = (array)$defaults;
57: }
58:
59: /**
60: * Parses a string URL into an array. Parsed URLs will result in an automatic
61: * redirection.
62: *
63: * @param string $url The URL to parse.
64: * @return false|null False on failure, null otherwise.
65: */
66: public function parse($url)
67: {
68: $params = parent::parse($url);
69: if (!$params) {
70: return false;
71: }
72: if (!$this->response) {
73: $this->response = new Response();
74: }
75: $redirect = $this->redirect;
76: if (count($this->redirect) === 1 && !isset($this->redirect['controller'])) {
77: $redirect = $this->redirect[0];
78: }
79: if (isset($this->options['persist']) && is_array($redirect)) {
80: $redirect += ['pass' => $params['pass'], 'url' => []];
81: if (is_array($this->options['persist'])) {
82: foreach ($this->options['persist'] as $elem) {
83: if (isset($params[$elem])) {
84: $redirect[$elem] = $params[$elem];
85: }
86: }
87: }
88: $redirect = Router::reverse($redirect);
89: }
90: $status = 301;
91: if (isset($this->options['status']) && ($this->options['status'] >= 300 && $this->options['status'] < 400)) {
92: $status = $this->options['status'];
93: }
94: $this->response->header([
95: 'Location' => Router::url($redirect, true)
96: ]);
97: $this->response->statusCode($status);
98: $this->response->send();
99: $this->response->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: * @param array $context Array of request context parameters.
107: * @return bool Always false.
108: */
109: public function match(array $url, array $context = [])
110: {
111: return false;
112: }
113: }
114: