Application.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\base\InvalidRouteException;
  10. use yii\helpers\Url;
  11. /**
  12. * Application is the base class for all web application classes.
  13. *
  14. * For more details and usage information on Application, see the [guide article on applications](guide:structure-applications).
  15. *
  16. * @property-read ErrorHandler $errorHandler The error handler application component. This property is
  17. * read-only.
  18. * @property string $homeUrl The homepage URL.
  19. * @property-read Request $request The request component. This property is read-only.
  20. * @property-read Response $response The response component. This property is read-only.
  21. * @property-read Session $session The session component. This property is read-only.
  22. * @property-read User $user The user component. This property is read-only.
  23. *
  24. * @author Qiang Xue <qiang.xue@gmail.com>
  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. * {@inheritdoc}
  58. */
  59. protected function bootstrap()
  60. {
  61. $request = $this->getRequest();
  62. Yii::setAlias('@webroot', dirname($request->getScriptFile()));
  63. Yii::setAlias('@web', $request->getBaseUrl());
  64. parent::bootstrap();
  65. }
  66. /**
  67. * Handles the specified request.
  68. * @param Request $request the request to be handled
  69. * @return Response the resulting response
  70. * @throws NotFoundHttpException if the requested route is invalid
  71. */
  72. public function handleRequest($request)
  73. {
  74. if (empty($this->catchAll)) {
  75. try {
  76. list($route, $params) = $request->resolve();
  77. } catch (UrlNormalizerRedirectException $e) {
  78. $url = $e->url;
  79. if (is_array($url)) {
  80. if (isset($url[0])) {
  81. // ensure the route is absolute
  82. $url[0] = '/' . ltrim($url[0], '/');
  83. }
  84. $url += $request->getQueryParams();
  85. }
  86. return $this->getResponse()->redirect(Url::to($url, $e->scheme), $e->statusCode);
  87. }
  88. } else {
  89. $route = $this->catchAll[0];
  90. $params = $this->catchAll;
  91. unset($params[0]);
  92. }
  93. try {
  94. Yii::debug("Route requested: '$route'", __METHOD__);
  95. $this->requestedRoute = $route;
  96. $result = $this->runAction($route, $params);
  97. if ($result instanceof Response) {
  98. return $result;
  99. }
  100. $response = $this->getResponse();
  101. if ($result !== null) {
  102. $response->data = $result;
  103. }
  104. return $response;
  105. } catch (InvalidRouteException $e) {
  106. throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e);
  107. }
  108. }
  109. private $_homeUrl;
  110. /**
  111. * @return string the homepage URL
  112. */
  113. public function getHomeUrl()
  114. {
  115. if ($this->_homeUrl === null) {
  116. if ($this->getUrlManager()->showScriptName) {
  117. return $this->getRequest()->getScriptUrl();
  118. }
  119. return $this->getRequest()->getBaseUrl() . '/';
  120. }
  121. return $this->_homeUrl;
  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. }