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.

Application.php 4.3KB

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