1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\helpers\Url; |
12
|
|
|
use yii\base\InvalidRouteException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Application is the base class for all web application classes. |
16
|
|
|
* |
17
|
|
|
* @property ErrorHandler $errorHandler The error handler application component. This property is read-only. |
18
|
|
|
* @property string $homeUrl The homepage URL. |
19
|
|
|
* @property Request $request The request component. This property is read-only. |
20
|
|
|
* @property Response $response The response component. This property is read-only. |
21
|
|
|
* @property Session $session The session component. This property is read-only. |
22
|
|
|
* @property User $user The user component. This property is read-only. |
23
|
|
|
* |
24
|
|
|
* @author Qiang Xue <[email protected]> |
25
|
|
|
* @since 2.0 |
26
|
|
|
*/ |
27
|
|
|
class Application extends \yii\base\Application |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string the default route of this application. Defaults to 'site'. |
31
|
|
|
*/ |
32
|
|
|
public $defaultRoute = 'site'; |
33
|
|
|
/** |
34
|
|
|
* @var array the configuration specifying a controller action which should handle |
35
|
|
|
* all user requests. This is mainly used when the application is in maintenance mode |
36
|
|
|
* and needs to handle all incoming requests via a single action. |
37
|
|
|
* The configuration is an array whose first element specifies the route of the action. |
38
|
|
|
* The rest of the array elements (key-value pairs) specify the parameters to be bound |
39
|
|
|
* to the action. For example, |
40
|
|
|
* |
41
|
|
|
* ```php |
42
|
|
|
* [ |
43
|
|
|
* 'offline/notice', |
44
|
|
|
* 'param1' => 'value1', |
45
|
|
|
* 'param2' => 'value2', |
46
|
|
|
* ] |
47
|
|
|
* ``` |
48
|
|
|
* |
49
|
|
|
* Defaults to null, meaning catch-all is not used. |
50
|
|
|
*/ |
51
|
|
|
public $catchAll; |
52
|
|
|
/** |
53
|
|
|
* @var Controller the currently active controller instance |
54
|
|
|
*/ |
55
|
|
|
public $controller; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
113 |
|
protected function bootstrap() |
62
|
|
|
{ |
63
|
113 |
|
$request = $this->getRequest(); |
64
|
113 |
|
Yii::setAlias('@webroot', dirname($request->getScriptFile())); |
65
|
113 |
|
Yii::setAlias('@web', $request->getBaseUrl()); |
66
|
|
|
|
67
|
113 |
|
parent::bootstrap(); |
68
|
113 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Handles the specified request. |
72
|
|
|
* @param Request $request the request to be handled |
73
|
|
|
* @return Response the resulting response |
74
|
|
|
* @throws NotFoundHttpException if the requested route is invalid |
75
|
|
|
*/ |
76
|
|
|
public function handleRequest($request) |
77
|
|
|
{ |
78
|
|
|
if (empty($this->catchAll)) { |
79
|
|
|
try { |
80
|
|
|
list ($route, $params) = $request->resolve(); |
81
|
|
|
} catch (UrlNormalizerRedirectException $e) { |
82
|
|
|
$url = $e->url; |
83
|
|
|
if (is_array($url)) { |
84
|
|
|
if (isset($url[0])) { |
85
|
|
|
// ensure the route is absolute |
86
|
|
|
$url[0] = '/' . ltrim($url[0], '/'); |
87
|
|
|
} |
88
|
|
|
$url += $request->getQueryParams(); |
89
|
|
|
} |
90
|
|
|
return $this->getResponse()->redirect(Url::to($url, $e->scheme), $e->statusCode); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
$route = $this->catchAll[0]; |
94
|
|
|
$params = $this->catchAll; |
95
|
|
|
unset($params[0]); |
96
|
|
|
} |
97
|
|
|
try { |
98
|
|
|
Yii::trace("Route requested: '$route'", __METHOD__); |
99
|
|
|
$this->requestedRoute = $route; |
100
|
|
|
$result = $this->runAction($route, $params); |
101
|
|
|
if ($result instanceof Response) { |
102
|
|
|
return $result; |
103
|
|
|
} else { |
104
|
|
|
$response = $this->getResponse(); |
105
|
|
|
if ($result !== null) { |
106
|
|
|
$response->data = $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $response; |
110
|
|
|
} |
111
|
|
|
} catch (InvalidRouteException $e) { |
112
|
|
|
throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private $_homeUrl; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string the homepage URL |
120
|
|
|
*/ |
121
|
3 |
|
public function getHomeUrl() |
122
|
|
|
{ |
123
|
3 |
|
if ($this->_homeUrl === null) { |
124
|
3 |
|
if ($this->getUrlManager()->showScriptName) { |
125
|
3 |
|
return $this->getRequest()->getScriptUrl(); |
126
|
|
|
} else { |
127
|
|
|
return $this->getRequest()->getBaseUrl() . '/'; |
128
|
|
|
} |
129
|
|
|
} else { |
130
|
|
|
return $this->_homeUrl; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $value the homepage URL |
136
|
|
|
*/ |
137
|
|
|
public function setHomeUrl($value) |
138
|
|
|
{ |
139
|
|
|
$this->_homeUrl = $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the error handler component. |
144
|
|
|
* @return ErrorHandler the error handler application component. |
145
|
|
|
*/ |
146
|
|
|
public function getErrorHandler() |
147
|
|
|
{ |
148
|
|
|
return $this->get('errorHandler'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Returns the request component. |
153
|
|
|
* @return Request the request component. |
154
|
|
|
*/ |
155
|
113 |
|
public function getRequest() |
156
|
|
|
{ |
157
|
113 |
|
return $this->get('request'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the response component. |
162
|
|
|
* @return Response the response component. |
163
|
|
|
*/ |
164
|
56 |
|
public function getResponse() |
165
|
|
|
{ |
166
|
56 |
|
return $this->get('response'); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the session component. |
171
|
|
|
* @return Session the session component. |
172
|
|
|
*/ |
173
|
26 |
|
public function getSession() |
174
|
|
|
{ |
175
|
26 |
|
return $this->get('session'); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns the user component. |
180
|
|
|
* @return User the user component. |
181
|
|
|
*/ |
182
|
23 |
|
public function getUser() |
183
|
|
|
{ |
184
|
23 |
|
return $this->get('user'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @inheritdoc |
189
|
|
|
*/ |
190
|
113 |
|
public function coreComponents() |
191
|
|
|
{ |
192
|
113 |
|
return array_merge(parent::coreComponents(), [ |
193
|
113 |
|
'request' => ['class' => Request::class], |
194
|
113 |
|
'response' => ['class' => Response::class], |
195
|
113 |
|
'session' => ['class' => Session::class], |
196
|
113 |
|
'user' => ['class' => User::class], |
197
|
113 |
|
'errorHandler' => ['class' => ErrorHandler::class], |
198
|
113 |
|
]); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|