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