This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
/
Redirect.php
170 lines (152 loc) · 4.64 KB
/
Redirect.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Mvc
*/
namespace Zend\Mvc\Controller\Plugin;
use Zend\Http\Response;
use Zend\Mvc\Exception;
use Zend\Mvc\InjectApplicationEventInterface;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteStackInterface;
/**
* @todo allow specifying status code as a default, or as an option to methods
* @category Zend
* @package Zend_Mvc
* @subpackage Controller
*/
class Redirect extends AbstractPlugin
{
protected $event;
protected $response;
protected $router;
/**
* Gets current route name
*
* @return string
*/
protected function getCurrentRoute ()
{
return $this->getEvent()->getRouteMatch()->getMatchedRouteName();
}
/**
* Gets current route params
*/
protected function getCurrentRouteParams()
{
return $this->getEvent()->getRouteMatch()->getParams();
}
/**
* Generates a URL based on a route
*
* @param string $route RouteInterface name
* @param array $params Parameters to use in url generation, if any
* @param array $options RouteInterface-specific options to use in url generation, if any
* @return Response
* @throws Exception\DomainException if composed controller does not implement InjectApplicationEventInterface, or
* router cannot be found in controller event
*/
public function toRoute($route = null, array $params = array(), array $options = array())
{
// Refresh if null provided
if (is_null($route)) {
$route = $this->getCurrentRoute();
$params = array_merge($this->getCurrentRouteParams(), $params);
}
$response = $this->getResponse();
$router = $this->getRouter();
$options['name'] = $route;
$url = $router->assemble($params, $options);
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
/**
* Redirect to the given URL
*
* @param string $url
* @return Response
*/
public function toUrl($url)
{
$response = $this->getResponse();
$response->getHeaders()->addHeaderLine('Location', $url);
$response->setStatusCode(302);
return $response;
}
/**
* Refresh to current route
*
* @return string
*/
public function refresh()
{
return $this->toRoute($this->getCurrentRoute(), $this->getCurrentRouteParams());
}
/**
* Get the router
*
* @return RouteStackInterface
* @throws Exception\DomainException if unable to find router
*/
protected function getRouter()
{
if ($this->router) {
return $this->router;
}
$event = $this->getEvent();
$router = $event->getRouter();
if (!$router instanceof RouteStackInterface) {
throw new Exception\DomainException('Redirect plugin requires event compose a router');
}
$this->router = $router;
return $this->router;
}
/**
* Get the response
*
* @return Response
* @throws Exception\DomainException if unable to find response
*/
protected function getResponse()
{
if ($this->response) {
return $this->response;
}
$event = $this->getEvent();
$response = $event->getResponse();
if (!$response instanceof Response) {
throw new Exception\DomainException('Redirect plugin requires event compose a response');
}
$this->response = $response;
return $this->response;
}
/**
* Get the event
*
* @return MvcEvent
* @throws Exception\DomainException if unable to find event
*/
protected function getEvent()
{
if ($this->event) {
return $this->event;
}
$controller = $this->getController();
if (!$controller instanceof InjectApplicationEventInterface) {
throw new Exception\DomainException('Redirect plugin requires a controller that implements InjectApplicationEventInterface');
}
$event = $controller->getEvent();
if (!$event instanceof MvcEvent) {
$params = $event->getParams();
$event = new MvcEvent();
$event->setParams($params);
}
$this->event = $event;
return $this->event;
}
}