Yii源码阅读笔记(三十二)
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'],
]);
}
}
Yii源码阅读笔记(三十二)的更多相关文章
- Yii源码阅读笔记(十二)
Action类,控制器中方法的基类: namespace yii\base; use Yii; /** * Action is the base class for all controller ac ...
- Yii源码阅读笔记(十八)
View中的查找视图文件方法和渲染文件方法 /** * Finds the view file based on the given view name. * 通过view文件名查找view文件 * ...
- Yii源码阅读笔记(十五)
Model类,集中整个应用的数据和业务逻辑——验证 /** * Returns the attribute labels. * 返回属性的标签 * * Attribute labels are mai ...
- Yii源码阅读笔记(十六)
Model类,集中整个应用的数据和业务逻辑—— /** * Generates a user friendly attribute label based on the give attribute ...
- Yii源码阅读笔记(十四)
Model类,集中整个应用的数据和业务逻辑——场景.属性和标签: /** * Returns a list of scenarios and the corresponding active attr ...
- Yii源码阅读笔记(十)
控制器类,所有控制器的基类,用于调用模型和布局,输出到视图 namespace yii\base; use Yii; /** * Controller is the base class for cl ...
- Yii源码阅读笔记(十九)
View中渲染view视图文件的前置和后置方法,以及渲染动态内容的方法: /** * @return string|boolean the view file currently being rend ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- Werkzeug源码阅读笔记(三)
这次主要讲下werkzeug中的Local. 源码在werkzeug/local.py Thread Local 在Python中,状态是保存在对象中.Thread Local是一种特殊的对象,它是对 ...
随机推荐
- 耿丹CS16-2班第一次作业汇总
第一次作业统计完成. 注:1.作业顺序:取最早交作业的前3名,依次拿5,2,1分,前提是作业质量较高,否则轮至下一名同学得分,其余同学得0分:2.作业情况:满10分,空一题扣2分,心得写得好的有额外加 ...
- php之获取程序源码的方法
文件hello.php和index.php在同一目录 hello.php <?php class hello{ public function __construct() { echo 'hel ...
- C语言 链表排序
#include <stdio.h> #include <stdlib.h> #include <assert.h> typedef struct node{ in ...
- 当你在浏览器地址栏输入一个URL后回车,将会发生的事情?
原文:http://igoro.com/archive/what-really-happens-when-you-navigate-to-a-url/ 作为一个软件开发者,你一定会对网络应用如何工作有 ...
- web前端学习部落22群开源分享 左边菜单导航
有大量web前端开发工具及学习资料,可以搜群[ web前端学习部落22群 ]进行下载,遇到学习问题也可以问群内专家以及课程老师哟 <!DOCTYPE html> <html lang ...
- Swift - 使用CAKeyframeAnimation实现关键帧动画
1,CAKeyframeAnimation介绍 CAKeyframeAnimation可以实现关键帧动画,这个类可以实现某一属性按照一串的数值进行动画,就像是一帧一帧的制作出来一样. 2,使用样例 ...
- 一键删除.svn文件bat脚本
用过SVN或CVS版本控制工具的朋友,在享受着它们给我们带来的方便的同时,也许也在为这么一件事情苦恼: 如果某个目录在SVN或CVS版本控制工具的控制之下时.该目录下以及该子孙目录下都会有一个.svn ...
- winform listview控件、容器控件
ListVies控件主要用于展示数据 常用属性: FullRowSelect:设置是否行选择模式.(默认为false) (开启之后一下选中一行数据) GridLines:设置行和列之间是否显示网格线. ...
- mux复用 demux解复用
保存音频包: 直接输出解复用之后的的音频数据码流.只需要在每次调用av_read_frame()之后将得到的音频的AVPacket存为本地文件即可. 但在分离AAC码流的时候,直接存储AVPacket ...
- js 一些容易错的点
最近在做平台相关功能,需要和js.html.以及使用bootstrap,bootstrap元素大小的设置 是比较难的. js比较容易出错的是,解析json,对象最好是使用json,这样更好解析,arr ...