1 | <?php |
||
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() |
|
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() |
|
133 | |||
134 | /** |
||
135 | * @param string $value the homepage URL |
||
136 | */ |
||
137 | public function setHomeUrl($value) |
||
141 | |||
142 | /** |
||
143 | * Returns the error handler component. |
||
144 | * @return ErrorHandler the error handler application component. |
||
145 | */ |
||
146 | public function getErrorHandler() |
||
150 | |||
151 | /** |
||
152 | * Returns the request component. |
||
153 | * @return Request the request component. |
||
154 | */ |
||
155 | 113 | public function getRequest() |
|
159 | |||
160 | /** |
||
161 | * Returns the response component. |
||
162 | * @return Response the response component. |
||
163 | */ |
||
164 | 56 | public function getResponse() |
|
168 | |||
169 | /** |
||
170 | * Returns the session component. |
||
171 | * @return Session the session component. |
||
172 | */ |
||
173 | 26 | public function getSession() |
|
177 | |||
178 | /** |
||
179 | * Returns the user component. |
||
180 | * @return User the user component. |
||
181 | */ |
||
182 | 23 | public function getUser() |
|
186 | |||
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | 113 | public function coreComponents() |
|
200 | } |
||
201 |