You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 satır
5.6KB

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