Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Application.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 ErrorHandler $errorHandler The error handler application 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 <qiang.xue@gmail.com>
  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. * ```php
  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. protected function bootstrap()
  56. {
  57. $request = $this->getRequest();
  58. Yii::setAlias('@webroot', dirname($request->getScriptFile()));
  59. Yii::setAlias('@web', $request->getBaseUrl());
  60. parent::bootstrap();
  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 = $this->catchAll;
  75. unset($params[0]);
  76. }
  77. try {
  78. Yii::trace("Route requested: '$route'", __METHOD__);
  79. $this->requestedRoute = $route;
  80. $result = $this->runAction($route, $params);
  81. if ($result instanceof Response) {
  82. return $result;
  83. } else {
  84. $response = $this->getResponse();
  85. if ($result !== null) {
  86. $response->data = $result;
  87. }
  88. return $response;
  89. }
  90. } catch (InvalidRouteException $e) {
  91. throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e);
  92. }
  93. }
  94. private $_homeUrl;
  95. /**
  96. * @return string the homepage URL
  97. */
  98. public function getHomeUrl()
  99. {
  100. if ($this->_homeUrl === null) {
  101. if ($this->getUrlManager()->showScriptName) {
  102. return $this->getRequest()->getScriptUrl();
  103. } else {
  104. return $this->getRequest()->getBaseUrl() . '/';
  105. }
  106. } else {
  107. return $this->_homeUrl;
  108. }
  109. }
  110. /**
  111. * @param string $value the homepage URL
  112. */
  113. public function setHomeUrl($value)
  114. {
  115. $this->_homeUrl = $value;
  116. }
  117. /**
  118. * Returns the error handler component.
  119. * @return ErrorHandler the error handler application component.
  120. */
  121. public function getErrorHandler()
  122. {
  123. return $this->get('errorHandler');
  124. }
  125. /**
  126. * Returns the request component.
  127. * @return Request the request component.
  128. */
  129. public function getRequest()
  130. {
  131. return $this->get('request');
  132. }
  133. /**
  134. * Returns the response component.
  135. * @return Response the response component.
  136. */
  137. public function getResponse()
  138. {
  139. return $this->get('response');
  140. }
  141. /**
  142. * Returns the session component.
  143. * @return Session the session component.
  144. */
  145. public function getSession()
  146. {
  147. return $this->get('session');
  148. }
  149. /**
  150. * Returns the user component.
  151. * @return User the user component.
  152. */
  153. public function getUser()
  154. {
  155. return $this->get('user');
  156. }
  157. /**
  158. * @inheritdoc
  159. */
  160. public function coreComponents()
  161. {
  162. return array_merge(parent::coreComponents(), [
  163. 'request' => ['class' => 'yii\web\Request'],
  164. 'response' => ['class' => 'yii\web\Response'],
  165. 'session' => ['class' => 'yii\web\Session'],
  166. 'user' => ['class' => 'yii\web\User'],
  167. 'errorHandler' => ['class' => 'yii\web\ErrorHandler'],
  168. ]);
  169. }
  170. }