1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 2.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Routing\Route;
16:
17: use Cake\Routing\Exception\RedirectException;
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: * Redirection is signalled by an exception that halts route matching and
26: * defines the redirect URL and status code.
27: */
28: class RedirectRoute extends Route
29: {
30: /**
31: * A Response object
32: *
33: * @var \Cake\Http\Response
34: * @deprecated 3.2.0 This property is unused.
35: */
36: public $response;
37:
38: /**
39: * The location to redirect to. Either a string or a CakePHP array URL.
40: *
41: * @var array|string
42: */
43: public $redirect;
44:
45: /**
46: * Constructor
47: *
48: * @param string $template Template string with parameter placeholders
49: * @param array|string $defaults Defaults for the route.
50: * @param array $options Array of additional options for the Route
51: */
52: public function __construct($template, $defaults = [], array $options = [])
53: {
54: parent::__construct($template, $defaults, $options);
55: if (is_array($defaults) && isset($defaults['redirect'])) {
56: $defaults = $defaults['redirect'];
57: }
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: * @param string $method The HTTP method being used.
67: * @return bool|null False on failure. An exception is raised on a successful match.
68: * @throws \Cake\Routing\Exception\RedirectException An exception is raised on successful match.
69: * This is used to halt route matching and signal to the middleware that a redirect should happen.
70: */
71: public function parse($url, $method = '')
72: {
73: $params = parent::parse($url, $method);
74: if (!$params) {
75: return false;
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 += ['pass' => $params['pass'], 'url' => []];
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::reverseToArray($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: throw new RedirectException(Router::url($redirect, true), $status);
97: }
98:
99: /**
100: * There is no reverse routing redirection routes.
101: *
102: * @param array $url Array of parameters to convert to a string.
103: * @param array $context Array of request context parameters.
104: * @return bool Always false.
105: */
106: public function match(array $url, array $context = [])
107: {
108: return false;
109: }
110:
111: /**
112: * Sets the HTTP status
113: *
114: * @param int $status The status code for this route
115: * @return $this
116: */
117: public function setStatus($status)
118: {
119: $this->options['status'] = $status;
120:
121: return $this;
122: }
123: }
124: