Application.php 4.5 KB

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