web/Application类的注释,继承base/Application类,针对web应用的一些处理:
namespace yii\web; use Yii; use yii\base\InvalidRouteException; /** * Application is the base class for all web application classes. * Application 是所有web应用的基类 * * @property string $homeUrl The homepage URL. * @property Session $session The session component. This property is read-only. * @property User $user The user component. This property is read-only. * * @author Qiang Xue <qiang.xue@gmail.com> * @since 2.0 */ class Application extends \yii\base\Application { /** * @var string the default route of this application. Defaults to 'site'. * @var string 默认的路由,默认值为‘site’. */ public $defaultRoute = 'site'; /** * @var array the configuration specifying a controller action which should handle * all user requests. This is mainly used when the application is in maintenance mode * and needs to handle all incoming requests via a single action. * @var array 配置项,指定一个控制器处理所有用户的请求,主要用于维护模式 * The configuration is an array whose first element specifies the route of the action. * The rest of the array elements (key-value pairs) specify the parameters to be bound * to the action. For example, * 配置项是一个第一个元素指定Action的路由,其余元素为参数的数组,例如: * * ```php * [ * 'offline/notice', * 'param1' => 'value1', * 'param2' => 'value2', * ] * ``` * * Defaults to null, meaning catch-all is not used. */ public $catchAll; /** * @var Controller the currently active controller instance * @var Controller 默认的控制器实例 */ public $controller; /** * @inheritdoc */ protected function bootstrap() { $request = $this->getRequest();//获取请求的组件 Yii::setAlias('@webroot', dirname($request->getScriptFile()));//设置webroot别名,该别名指向正在运行的应用的入口文件 index.php 所在的目录 Yii::setAlias('@web', $request->getBaseUrl());//定义别名web,指向当前应用的根URL,主要用于前端 parent::bootstrap();//调用父类方法,初始化应用组件 } /** * Handles the specified request. * 处理指定的请求 * @param Request $request the request to be handled * @return Response the resulting response * @throws NotFoundHttpException if the requested route is invalid */ public function handleRequest($request) { if (empty($this->catchAll)) { list ($route, $params) = $request->resolve();//取出路由及参数 } else { $route = $this->catchAll[0]; $params = $this->catchAll; unset($params[0]); } try { Yii::trace("Route requested: '$route'", __METHOD__); $this->requestedRoute = $route; $result = $this->runAction($route, $params);//运行控制器中的Acition if ($result instanceof Response) { return $result; } else { /** *这个是加载yii\base\Response类,在外部可以Yii::$app->get('response')、Yii::$app->getResponse()、Yii::$app->response 等等方式来加载response类 *主要用来加载http状态,及头信息,如301,302,404,ajax头等等的获取 */ $response = $this->getResponse(); if ($result !== null) { $response->data = $result; } return $response; } } catch (InvalidRouteException $e) { throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'), $e->getCode(), $e); } } private $_homeUrl; /** * @return string the homepage URL * @return string 返回主页的URL */ public function getHomeUrl() { if ($this->_homeUrl === null) { if ($this->getUrlManager()->showScriptName) {//如果请求路径中显示入口脚本 return $this->getRequest()->getScriptUrl();//返回入口脚本的相对路径 } else { return $this->getRequest()->getBaseUrl() . '/';//否则返回BaseUrl,即去掉入口脚本名和结束斜线的路径 } } else { return $this->_homeUrl; } } /** * @param string $value the homepage URL * 设置主页URL */ public function setHomeUrl($value) { $this->_homeUrl = $value; } /** * Returns the session component. * 返回session组件对象 * @return Session the session component. */ public function getSession() { return $this->get('session'); } /** * Returns the user component. * 返回user组件对象 * @return User the user component. */ public function getUser() { return $this->get('user'); } /** * @inheritdoc * 定义核心组件,用于程序初始化时加载 */ public function coreComponents() { return array_merge(parent::coreComponents(), [ 'request' => ['class' => 'yii\web\Request'], 'response' => ['class' => 'yii\web\Response'], 'session' => ['class' => 'yii\web\Session'], 'user' => ['class' => 'yii\web\User'], 'errorHandler' => ['class' => 'yii\web\ErrorHandler'], ]); } }