| 包 | system.base |
|---|---|
| 继承 | abstract class CApplication » CModule » CComponent |
| 子类 | CConsoleApplication, CWebApplication |
| 源自 | 1.0 |
| 版本 | $Id: CApplication.php 3515 2011-12-28 12:29:24Z mDOMba $ |
| 源码 |
一个应用程序服务在用户请求处理的全局范围内。 它负责为整个应用程序管理 提供具体功能的应用程序组件。
CApplication所提供的核心应用组件如下:
- errorHandler: 处理PHP错误和错误异常。 这个应用组件在需要时动态加载。
- securityManager: 提供安全相关的服务, 例如,哈希,加密功能。 这个应用组件在需要时动态加载。
- statePersister: 提供全局的持久状态方法。 这个应用组件在需要时动态加载。
- cache: 提供缓存功能。 这个应用组件默认是禁止的。
- messages: 为翻译应用程序信息提供信息源。 这个应用组件在需要时动态加载。
- coreMessages: 为翻译Yii框架信息提供信息源, 这个应用组件在需要时动态加载。
当处理用户请求时,CApplication将经历如下的生命周期:
- 加载应用程序配置;
- 设置类自动加载器和错误处理;
- 加载静态应用组件;
- onBeginRequest: 处理用户请求前;
- processRequest: 处理用户请求前;
- onEndRequest: 用户请求后处理(或后置处理用户请求);
从生命周期第3步开始,如果发生一个PHP错误或错误异常, 应用程序将切换到它的错误处理逻辑,并跳到第6步之后。
公共属性
| 属性 | 类型 | 描述 | 定义在 |
|---|---|---|---|
| basePath | string | 返回应用程序的根目录。 | CApplication |
| baseUrl | string | 返回应用程序的相对URL。 | CApplication |
| behaviors | array | the behaviors that should be attached to the module. | CModule |
| cache | CCache | 返回缓存组件。 | CApplication |
| charset | string | 应用程序当前使用的字符集。默认为‘UTF-8’。 | CApplication |
| components | array | 返回应用组件。 | CModule |
| controller | CController | 当前活动控制器。为Null返回它的基类。 | CApplication |
| coreMessages | CPhpMessageSource | 返回核心信息翻译组件。 | CApplication |
| dateFormatter | CDateFormatter | 返回本地化的日期格式。 | CApplication |
| db | CDbConnection | 返回数据库连接组件。 | CApplication |
| errorHandler | CErrorHandler | 返回错误处理组件。 | CApplication |
| extensionPath | string | 返回第三方扩展根目录。 | CApplication |
| homeUrl | string | 主页URL | CApplication |
| id | string | 返回应用程序的唯一标识符。 | CApplication |
| language | string | 返回用户正在使用的应用程序语言。 | CApplication |
| locale | CLocale | 返回本地(环境)实例。 | CApplication |
| localeDataPath | string | 返回包含本地化的数据目录。 | CApplication |
| messages | CMessageSource | 返回应用程序信息翻译组件。 | CApplication |
| modulePath | string | 返回包含应用程序模块的目录。 | CModule |
| modules | array | 返回应用程序当前已经安装的模块。 | CModule |
| name | string | 应用程序名字。默认为‘My Application’。 | CApplication |
| numberFormatter | CNumberFormatter | 本地化数字格式。 将使用当前application locale。 | CApplication |
| params | CAttributeCollection | 返回用户定义的参数。 | CModule |
| parentModule | CModule | 返回父模块。 | CModule |
| preload | array | the IDs of the application components that should be preloaded. | CModule |
| request | CHttpRequest | 返回请求组件。 | CApplication |
| runtimePath | string | 返回存储运行时文件目录。 | CApplication |
| securityManager | CSecurityManager | 返回安全管理器组件。 | CApplication |
| sourceLanguage | string | 应用程序使用语言. | CApplication |
| statePersister | CStatePersister | 返回持久状态组件。 | CApplication |
| timeZone | string | 返回应用程序所使用的时区。 | CApplication |
| urlManager | CUrlManager | 返回URL管理器组件。 | CApplication |
公共方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| __call() | 如果类中没有调的方法名,则调用这个方法。 | CComponent |
| __construct() | 构造器。 | CApplication |
| __get() | getter魔术方法。 | CModule |
| __isset() | 检查一个属性值是否为null。 | CModule |
| __set() | 设置一个组件的属性值。 | CComponent |
| __unset() | 设置一个组件的属性为null。 | CComponent |
| asa() | 返回这个名字的行为对象。 | CComponent |
| attachBehavior() | 附加一个行为到组件。 | CComponent |
| attachBehaviors() | 附加一个行为列表到组件。 | CComponent |
| attachEventHandler() | 为事件附加一个事件处理程序。 | CComponent |
| canGetProperty() | 确定属性是否可读。 | CComponent |
| canSetProperty() | 确定属性是否可写。 | CComponent |
| clearGlobalState() | 清空一个全局值。 | CApplication |
| configure() | 为模块指定配置。 | CModule |
| createAbsoluteUrl() | 创建一个基于给定控制器和动作(信息)绝对的URL。 | CApplication |
| createUrl() | 创建一个基于给定控制器和动作(信息)相对的URL。 | CApplication |
| detachBehavior() | 从组件中分离一个行为。 | CComponent |
| detachBehaviors() | 从组件中分离所有行为。 | CComponent |
| detachEventHandler() | 分离一个存在的事件处理程序。 | CComponent |
| disableBehavior() | 禁用一个附加行为。 | CComponent |
| disableBehaviors() | 禁用组件附加的所有行为。 | CComponent |
| displayError() | 显示捕获的PHP错误。 | CApplication |
| displayException() | 显示未捕获的PHP异常。 | CApplication |
| enableBehavior() | 启用一个附加行为。 | CComponent |
| enableBehaviors() | 启用组件附加的所有行为。 | CComponent |
| end() | 终止应用程序。 | CApplication |
| evaLuateExpression() | 计算一个PHP表达式,或根据组件上下文执行回调。 | CComponent |
| findLocalizedFile() | 返回指定文件的本地化版本。 | CApplication |
| getBasePath() | 返回应用程序的根目录。 | CApplication |
| getBaseUrl() | 返回应用程序的相对URL。 | CApplication |
| getCache() | 返回缓存组件。 | CApplication |
| getComponent() | Retrieves the named application component. | CModule |
| getComponents() | 返回应用组件。 | CModule |
| getController() | 返回当前活动控制器。为Null返回它的基类。 | CApplication |
| getCoreMessages() | 返回核心信息翻译组件。 | CApplication |
| getDateFormatter() | 返回本地化的日期格式。 | CApplication |
| getDb() | 返回数据库连接组件。 | CApplication |
| getErrorHandler() | 返回错误处理组件。 | CApplication |
| getEventHandlers() | 返回一个事件的附加处理程序列表。 | CComponent |
| getExtensionPath() | 返回第三方扩展根目录。 | CApplication |
| getGlobalState() | 返回一个全局值 | CApplication |
| getHomeUrl() | 返回主页URL | CApplication |
| getId() | 返回应用程序的唯一标识符。 | CApplication |
| getLanguage() | 返回用户正在使用的应用程序语言。 | CApplication |
| getLocale() | 返回本地(环境)实例。 | CApplication |
| getLocaleDataPath() | 返回包含本地化的数据目录。 | CApplication |
| getMessages() | 返回应用程序信息翻译组件。 | CApplication |
| getModule() | 检索指定的应用模块。 | CModule |
| getModulePath() | 返回包含应用程序模块的目录。 | CModule |
| getModules() | 返回应用程序当前已经安装的模块。 | CModule |
| getNumberFormatter() | 返回本地化数字格式。 将使用当前application locale。 | CApplication |
| getParams() | 返回用户定义的参数。 | CModule |
| getParentModule() | 返回父模块。 | CModule |
| getRequest() | 返回请求组件。 | CApplication |
| getRuntimePath() | 返回存储运行时文件目录。 | CApplication |
| getSecurityManager() | 返回安全管理器组件。 | CApplication |
| getStatePersister() | 返回持久状态组件。 | CApplication |
| getTimeZone() | 返回应用程序所使用的时区。 | CApplication |
| getUrlManager() | 返回URL管理器组件。 | CApplication |
| handleError() | 处理PHP异常错误,如警告,通知。 | CApplication |
| handleException() | 处理未捕获的PHP异常。 | CApplication |
| hasComponent() | 检查是否存在指定组件。 | CModule |
| hasEvent() | 确定一个事件是否定义。 | CComponent |
| hasEventHandler() | 检查事件是否有附加的处理程序。 | CComponent |
| hasModule() | 检查是否已经安装了指定模块。 | CModule |
| hasProperty() | 确定属性是否被定义。 | CComponent |
| loadGlobalState() | 从持久存储加载全局状态数据。 | CApplication |
| onBeginRequest() | 应用程序处理请求之前发起。 | CApplication |
| onEndRequest() | 应用程序处理请求之后发起。 | CApplication |
| onError() | 当一个PHP异常错误发生时发起。 | CApplication |
| onException() | 当一个未捕获的PHP异常发生时发起。 | CApplication |
| processRequest() | 处理请求。 | CApplication |
| raiseEvent() | 发起一个事件。 | CComponent |
| run() | 运行应用程序。 | CApplication |
| saveGlobalState() | 保存全局状态数据到持久存储。 | CApplication |
| setAliases() | 定义根目录的别名。 | CModule |
| setBasePath() | 设置应用程序根目录。 | CApplication |
| setComponent() | 添加一个组件到模块中 | CModule |
| setComponents() | 设置应用组件。 | CModule |
| setExtensionPath() | 设置第三方扩展目录。 | CApplication |
| setGlobalState() | 设置一个全局值。 | CApplication |
| setHomeUrl() | 设置设置主页URL | CApplication |
| setId() | 设置应用程序唯一的标识符。 | CApplication |
| setImport() | 设置在模块中使用的路径别名。 | CModule |
| setLanguage() | 指定应用程序语言。 | CApplication |
| setLocaleDataPath() | 设置包含本地化数据的目录。 | CApplication |
| setModulePath() | 设置包含应用程序模块的目录。 | CModule |
| setModules() | 配置模块的子模块。 | CModule |
| setParams() | 设置用户定义的参数。 | CModule |
| setRuntimePath() | 设置存储运行时文件目录。 | CApplication |
| setTimeZone() | 设置应用程序所使用的时区。 | CApplication |
受保护方法
| 方法 | 描述 | 定义在 |
|---|---|---|
| init() | 初始化模块。 | CModule |
| initSystemHandlers() | 初始化类自动加载器和错误处理。 | CApplication |
| preinit() | 模块预初始化。 | CModule |
| preloadComponents() | 加载静态应用组件。 | CModule |
| registerCoreComponents() | 注册核心应用组件。 | CApplication |
事件
| 事件 | 描述 | 定义在 |
|---|---|---|
| onBeginRequest | 应用程序处理请求之前发起。 | CApplication |
| onEndRequest | 应用程序处理请求之后发起。 | CApplication |
| onException | 当一个未捕获的PHP异常发生时发起。 | CApplication |
| onError | 当一个PHP异常错误发生时发起。 | CApplication |
属性详细
public void setBasePath(string $path)
返回应用程序的根目录。
返回应用程序的相对URL。 快捷方法就是CHttpRequest::getBaseUrl()。
参见
- CHttpRequest::getBaseUrl()
返回缓存组件。
应用程序当前使用的字符集。默认为‘UTF-8’。
当前活动控制器。为Null返回它的基类。
返回核心信息翻译组件。
返回本地化的日期格式。
返回数据库连接组件。
返回错误处理组件。
public void setExtensionPath(string $path)
返回第三方扩展根目录。
public void setHomeUrl(string $value)
主页URL
public void setId(string $id)
返回应用程序的唯一标识符。
public void setLanguage(string $language)
返回用户正在使用的应用程序语言。
返回本地(环境)实例。
public void setLocaleDataPath(string $value)
返回包含本地化的数据目录。
返回应用程序信息翻译组件。
应用程序名字。默认为‘My Application’。
本地化数字格式。 将使用当前application locale。
返回请求组件。
public void setRuntimePath(string $path)
返回存储运行时文件目录。
返回安全管理器组件。
应用程序使用语言. 这个语言主要指信息和视图文件中的语言。 默认是‘en_us’(US English)。
返回持久状态组件。
public void setTimeZone(string $value)
返回应用程序所使用的时区。 这个一个简单的PHP函数date_default_timezone_get()的封装。
参见
- http://php.net/manual/en/function.date-default-timezone-get.php
返回URL管理器组件。
方法详细
|
public void __construct(mixed $config=NULL)
| ||
| $config | mixed | 应用程序配置。 如果是一个字符串,它将被当作包含配置的文件路径; 如果是一个数组,它将被当作具体的配置信息, 你确保在配置文件中指定basePath属性, 它通常应该指向含所有的应用程序逻辑、模板和数据的目录包。 如果没有,那么默认将指定为‘protected’目录。 |
public function __construct($config=null)
{
Yii::setApplication($this);
// set basePath at early as possible to avoid trouble
if(is_string($config))
$config=require($config);
if(isset($config['basePath']))
{
$this->setBasePath($config['basePath']);
unset($config['basePath']);
}
else
$this->setBasePath('protected');
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
$this->preinit();
$this->initSystemHandlers();
$this->registerCoreComponents();
$this->configure($config);
$this->attachBehaviors($this->behaviors);
$this->preloadComponents();
$this->init();
}
构造器。
|
public void clearGlobalState(string $key)
| ||
| $key | string | 要清除的值的名称 |
public function clearGlobalState($key)
{
$this->setGlobalState($key,true,true);
}
清空一个全局值。
被清除的值在这次请求或随后的请求中不可使用。
|
public string createAbsoluteUrl(string $route, array $params=array (
), string $schema='', string $ampersand='&')
| ||
| $route | string | URL路由。它的格式应该是:‘ControllerID/ActionID’。 |
| $params | array | 附加的GET参数(name=>value)。名称和值会被URL-encoded(URL编码)。 |
| $schema | string | 要使用的模式(例如: http,https)。如果为空,schema使用当前请求使用的模式。 |
| $ampersand | string | URL中name-value对的分隔标记。 |
| {return} | string | 已构造的URL |
public function createAbsoluteUrl($route,$params=array(),$schema='',$ampersand='&')
{
$url=$this->createUrl($route,$params,$ampersand);
if(strpos($url,'http')===0)
return $url;
else
return $this->getRequest()->getHostInfo($schema).$url;
}
创建一个基于给定控制器和动作(信息)绝对的URL。
|
public string createUrl(string $route, array $params=array (
), string $ampersand='&')
| ||
| $route | string | URL路由。它的格式应该是:‘ControllerID/ActionID’。 |
| $params | array | 附加的GET参数(name=>value)。名称和值会被URL-encoded(URL编码)。 |
| $ampersand | string | URL中name-value对的分隔标记。 |
| {return} | string | 已构造的URL |
public function createUrl($route,$params=array(),$ampersand='&')
{
return $this->getUrlManager()->createUrl($route,$params,$ampersand);
}
创建一个基于给定控制器和动作(信息)相对的URL。
|
public void displayError(integer $code, string $message, string $file, string $line)
| ||
| $code | integer | 错误代码 |
| $message | string | 错误信息 |
| $file | string | error 错误文件 |
| $line | string | error 错误行号 |
public function displayError($code,$message,$file,$line)
{
if(YII_DEBUG)
{
echo "<h1>PHP Error [$code]</h1>\n";
echo "<p>$message ($file:$line)</p>\n";
echo '<pre>';
$trace=debug_backtrace();
// skip the first 3 stacks as they do not tell the error position
if(count($trace)>3)
$trace=array_slice($trace,3);
foreach($trace as $i=>$t)
{
if(!isset($t['file']))
$t['file']='unknown';
if(!isset($t['line']))
$t['line']=0;
if(!isset($t['function']))
$t['function']='unknown';
echo "#$i {$t['file']}({$t['line']}): ";
if(isset($t['object']) && is_object($t['object']))
echo get_class($t['object']).'->';
echo "{$t['function']}()\n";
}
echo '</pre>';
}
else
{
echo "<h1>PHP Error [$code]</h1>\n";
echo "<p>$message</p>\n";
}
}
显示捕获的PHP错误。 当没有激活错误处理时, 这个方将在HTML显示错误。
|
public void displayException(Exception $exception)
| ||
| $exception | Exception | 未捕获的异常 |
public function displayException($exception)
{
if(YII_DEBUG)
{
echo '<h1>'.get_class($exception)."</h1>\n";
echo '<p>'.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().')</p>';
echo '<pre>'.$exception->getTraceAsString().'</pre>';
}
else
{
echo '<h1>'.get_class($exception)."</h1>\n";
echo '<p>'.$exception->getMessage().'</p>';
}
}
显示未捕获的PHP异常。 当没有激活错误处理时, 这个方法将在HTML显示未捕获异常。
|
public void end(integer $status=0, boolean $exit=true)
| ||
| $status | integer | 退出状态(0代表正常退出,其它值表示异常退出)。 |
| $exit | boolean | 是否要退出当前的请求。这个参数可用自1.1.5。 如果默认为ture,意味着将在这个方法结束时调用PHP的exit()函数。 |
public function end($status=0, $exit=true)
{
if($this->hasEventHandler('onEndRequest'))
$this->onEndRequest(new CEvent($this));
if($exit)
exit($status);
}
终止应用程序。 这个方法通过在退出前调用onEndRequest 替换PHP的exit()方法。
|
public string findLocalizedFile(string $srcFile, string $srcLanguage=NULL, string $language=NULL)
| ||
| $srcFile | string | 原文件 |
| $srcLanguage | string | 原文件中的语言,如果为null,将使用source language。 |
| $language | string | 所需要的本地语言.如果为null, 将使用application language。 |
| {return} | string | 匹配的本地化文件。如果没有本地化文件或如果源语言和要求语言相同, 将返回原始文件。 |
public function findLocalizedFile($srcFile,$srcLanguage=null,$language=null)
{
if($srcLanguage===null)
$srcLanguage=$this->sourceLanguage;
if($language===null)
$language=$this->getLanguage();
if($language===$srcLanguage)
return $srcFile;
$desiredFile=dirname($srcFile).DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.basename($srcFile);
return is_file($desiredFile) ? $desiredFile : $srcFile;
}
返回指定文件的本地化版本。
基于指定语言代码搜索。
特别是,具有相同名称的文件将在以本地ID命名子目录下查找。
例如。给定文件“path/to/view.php”和本地ID标识:“zh_cn”,
本地化文件将以这种形式查找“path/to/zh_cn/view.php”。
如果该文件没有找到,
将返回原始文件。
为了保持一致性,建议本地语言id使用小写格式
LanguageID_RegionID(例如,“en_us”)。
|
public string getBasePath()
| ||
| {return} | string | 应用程序的根目录。默认为‘protected’。 |
public function getBasePath()
{
return $this->_basePath;
}
返回应用程序的根目录。
|
public string getBaseUrl(boolean $absolute=false)
| ||
| $absolute | boolean | 是否返回一个绝对URL。默认为false,意味着返回一个相对的URL。 |
| {return} | string | 相关URL的应用。 |
public function getBaseUrl($absolute=false)
{
return $this->getRequest()->getBaseUrl($absolute);
}
返回应用程序的相对URL。 快捷方法就是CHttpRequest::getBaseUrl()。
参见
- CHttpRequest::getBaseUrl()
|
public CCache getCache()
| ||
| {return} | CCache | 应用程序缓存组件。null为不启用这个组件。 |
public function getCache()
{
return $this->getComponent('cache');
}
返回缓存组件。
|
public CController getController()
| ||
| {return} | CController | 当前活动控制器。为Null返回它的基类。 |
public function getController()
{
return null;
}
|
public CPhpMessageSource getCoreMessages()
| ||
| {return} | CPhpMessageSource | 核心信息翻译 |
public function getCoreMessages()
{
return $this->getComponent('coreMessages');
}
返回核心信息翻译组件。
|
public CDateFormatter getDateFormatter()
| ||
| {return} | CDateFormatter | 本地化的日期格式。 将会使用当前application locale。 |
public function getDateFormatter()
{
return $this->getLocale()->getDateFormatter();
}
返回本地化的日期格式。
|
public CDbConnection getDb()
| ||
| {return} | CDbConnection | 数据库连接 |
public function getDb()
{
return $this->getComponent('db');
}
返回数据库连接组件。
|
public CErrorHandler getErrorHandler()
| ||
| {return} | CErrorHandler | 错误处理应用程序组件。 |
public function getErrorHandler()
{
return $this->getComponent('errorHandler');
}
返回错误处理组件。
|
public string getExtensionPath()
| ||
| {return} | string | 包含所有扩展的目录。默认是‘protected’下的‘extensions’目录。 |
public function getExtensionPath()
{
return Yii::getPathOfAlias('ext');
}
返回第三方扩展根目录。
|
public mixed getGlobalState(string $key, mixed $defaultValue=NULL)
| ||
| $key | string | 要返回的值的名称 |
| $defaultValue | mixed | 默认值。如果该名称的全局值没有找到,将返回默认值。 |
| {return} | mixed | 全局值的名称 |
public function getGlobalState($key,$defaultValue=null)
{
if($this->_globalState===null)
$this->loadGlobalState();
if(isset($this->_globalState[$key]))
return $this->_globalState[$key];
else
return $defaultValue;
}
返回一个全局值
一个全局值,持续在是用户的sessions与requests之间。
参见
- setGlobalState
|
public string getHomeUrl()
| ||
| {return} | string | 主页URL |
public function getHomeUrl()
{
if($this->_homeUrl===null)
{
if($this->getUrlManager()->showScriptName)
return $this->getRequest()->getScriptUrl();
else
return $this->getRequest()->getBaseUrl().'/';
}
else
return $this->_homeUrl;
}
|
public string getId()
| ||
| {return} | string | $id 应用程序唯一的标识符。 |
public function getId()
{
if($this->_id!==null)
return $this->_id;
else
return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name));
}
返回应用程序的唯一标识符。
|
public string getLanguage()
| ||
| {return} | string | 用户正在使用的应用程序语言。 默认为source language。 |
public function getLanguage()
{
return $this->_language===null ? $this->sourceLanguage : $this->_language;
}
返回用户正在使用的应用程序语言。
|
public CLocale getLocale(string $localeID=NULL)
| ||
| $localeID | string | 本地(环境)实例ID(例如,en_US)。如果null,将使用application language ID。 |
| {return} | CLocale | 本地(环境)实例 |
public function getLocale($localeID=null)
{
return CLocale::getInstance($localeID===null?$this->getLanguage():$localeID);
}
返回本地(环境)实例。
|
public string getLocaleDataPath()
| ||
| {return} | string | 包含本地的数据化目录。默认为‘framework/i18n/data’。 |
public function getLocaleDataPath()
{
return CLocale::$dataPath===null ? Yii::getPathOfAlias('system.i18n.data') : CLocale::$dataPath;
}
返回包含本地化的数据目录。
|
public CMessageSource getMessages()
| ||
| {return} | CMessageSource | 应用程序信息翻译组件 |
public function getMessages()
{
return $this->getComponent('messages');
}
返回应用程序信息翻译组件。
|
public CNumberFormatter getNumberFormatter()
| ||
| {return} | CNumberFormatter | 本地化数字格式。 将使用当前application locale。 |
public function getNumberFormatter()
{
return $this->getLocale()->getNumberFormatter();
}
|
public CHttpRequest getRequest()
| ||
| {return} | CHttpRequest | 请求组件 |
public function getRequest()
{
return $this->getComponent('request');
}
返回请求组件。
|
public string getRuntimePath()
| ||
| {return} | string | 存储运行时文件目录。默认为‘protected/runtime’。 |
public function getRuntimePath()
{
if($this->_runtimePath!==null)
return $this->_runtimePath;
else
{
$this->setRuntimePath($this->getBasePath().DIRECTORY_SEPARATOR.'runtime');
return $this->_runtimePath;
}
}
返回存储运行时文件目录。
|
public CSecurityManager getSecurityManager()
| ||
| {return} | CSecurityManager | 安全管理器组件。 |
public function getSecurityManager()
{
return $this->getComponent('securityManager');
}
返回安全管理器组件。
|
public CStatePersister getStatePersister()
| ||
| {return} | CStatePersister | 持久状态组件。 |
public function getStatePersister()
{
return $this->getComponent('statePersister');
}
返回持久状态组件。
|
public string getTimeZone()
| ||
| {return} | string | 应用程序使用的时区。 |
public function getTimeZone()
{
return date_default_timezone_get();
}
返回应用程序所使用的时区。 这个一个简单的PHP函数date_default_timezone_get()的封装。
参见
- http://php.net/manual/en/function.date-default-timezone-get.php
|
public CUrlManager getUrlManager()
| ||
| {return} | CUrlManager | URL管理器组件 |
public function getUrlManager()
{
return $this->getComponent('urlManager');
}
返回URL管理器组件。
|
public void handleError(integer $code, string $message, string $file, integer $line)
| ||
| $code | integer | 发起错误的等级 |
| $message | string | 发起错误的信息 |
| $file | string | 发起错误的文件 |
| $line | integer | 发起错误的行号 |
public function handleError($code,$message,$file,$line)
{
if($code & error_reporting())
{
// disable error capturing to avoid recursive errors
restore_error_handler();
restore_exception_handler();
$log="$message ($file:$line)\nStack trace:\n";
$trace=debug_backtrace();
// skip the first 3 stacks as they do not tell the error position
if(count($trace)>3)
$trace=array_slice($trace,3);
foreach($trace as $i=>$t)
{
if(!isset($t['file']))
$t['file']='unknown';
if(!isset($t['line']))
$t['line']=0;
if(!isset($t['function']))
$t['function']='unknown';
$log.="#$i {$t['file']}({$t['line']}): ";
if(isset($t['object']) && is_object($t['object']))
$log.=get_class($t['object']).'->';
$log.="{$t['function']}()\n";
}
if(isset($_SERVER['REQUEST_URI']))
$log.='REQUEST_URI='.$_SERVER['REQUEST_URI'];
Yii::log($log,CLogger::LEVEL_ERROR,'php');
try
{
Yii::import('CErrorEvent',true);
$event=new CErrorEvent($this,$code,$message,$file,$line);
$this->onError($event);
if(!$event->handled)
{
// try an error handler
if(($handler=$this->getErrorHandler())!==null)
$handler->handle($event);
else
$this->displayError($code,$message,$file,$line);
}
}
catch(Exception $e)
{
$this->displayException($e);
}
try
{
$this->end(1);
}
catch(Exception $e)
{
// use the most primitive way to log error
$msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
$msg .= $e->getTraceAsString()."\n";
$msg .= "Previous error:\n";
$msg .= $log."\n";
$msg .= '$_SERVER='.var_export($_SERVER,true);
error_log($msg);
exit(1);
}
}
}
处理PHP异常错误,如警告,通知。
这个方法实现了一个PHP的错误处理。
它需要常量YII_ENABLE_ERROR_HANDLER被定义为true。
这个方法将首先发起一个onError事件。
如果该错误没有被任何事件处理程序处理,它将调用
errorHandler处理错误。
应用程序将被此方法终止。
|
public void handleException(Exception $exception)
| ||
| $exception | Exception | 异常没有被捕获 |
public function handleException($exception)
{
// disable error capturing to avoid recursive errors
restore_error_handler();
restore_exception_handler();
$category='exception.'.get_class($exception);
if($exception instanceof CHttpException)
$category.='.'.$exception->statusCode;
// php <5.2 doesn't support string conversion auto-magically
$message=$exception->__toString();
if(isset($_SERVER['REQUEST_URI']))
$message.="\nREQUEST_URI=".$_SERVER['REQUEST_URI'];
if(isset($_SERVER['HTTP_referer']))
$message.="\nHTTP_REFERER=".$_SERVER['HTTP_REFERER'];
$message.="\n---";
Yii::log($message,CLogger::LEVEL_ERROR,$category);
try
{
$event=new CExceptionEvent($this,$exception);
$this->onException($event);
if(!$event->handled)
{
// try an error handler
if(($handler=$this->getErrorHandler())!==null)
$handler->handle($event);
else
$this->displayException($exception);
}
}
catch(Exception $e)
{
$this->displayException($e);
}
try
{
$this->end(1);
}
catch(Exception $e)
{
// use the most primitive way to log error
$msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
$msg .= $e->getTraceAsString()."\n";
$msg .= "Previous exception:\n";
$msg .= get_class($exception).': '.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().")\n";
$msg .= $exception->getTraceAsString()."\n";
$msg .= '$_SERVER='.var_export($_SERVER,true);
error_log($msg);
exit(1);
}
}
处理未捕获的PHP异常。
这个方法实现了一个PHP的异常处理。
它需要它需要常量YII_ENABLE_EXCEPTION_HANDLER被定义为true.
这个方法将首先发起一个onException事件。
如果异常没有被任何事件处理程序处理,它将调用
errorHandler处理异常。
应用程序将被此方法终止。
|
protected void initSystemHandlers()
|
protected function initSystemHandlers()
{
if(YII_ENABLE_EXCEPTION_HANDLER)
set_exception_handler(array($this,'handleException'));
if(YII_ENABLE_ERROR_HANDLER)
set_error_handler(array($this,'handleError'),error_reporting());
}
初始化类自动加载器和错误处理。
|
public void loadGlobalState()
|
public function loadGlobalState()
{
$persister=$this->getStatePersister();
if(($this->_globalState=$persister->load())===null)
$this->_globalState=array();
$this->_stateChanged=false;
$this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
}
从持久存储加载全局状态数据。
参见
- getStatePersister
|
public void onBeginRequest(CEvent $event)
| ||
| $event | CEvent | 事件参数 |
public function onBeginRequest($event)
{
$this->raiseEvent('onBeginRequest',$event);
}
应用程序处理请求之前发起。
|
public void onEndRequest(CEvent $event)
| ||
| $event | CEvent | 事件参数 |
public function onEndRequest($event)
{
if(!$this->_ended)
{
$this->_ended=true;
$this->raiseEvent('onEndRequest',$event);
}
}
应用程序处理请求之后发起。
|
public void onError(CErrorEvent $event)
| ||
| $event | CErrorEvent | 事件参数 |
public function onError($event)
{
$this->raiseEvent('onError',$event);
}
当一个PHP异常错误发生时发起。
可以设置一个事件处理handled,
事件参数的属性为true表示不再需要进一步处理错误。
否则,errorHandler
应用组件将继续处理错误。
|
public void onException(CExceptionEvent $event)
| ||
| $event | CExceptionEvent | 事件参数 |
public function onException($event)
{
$this->raiseEvent('onException',$event);
}
当一个未捕获的PHP异常发生时发起。
可以设置一个事件处理handled,
事件参数的属性为true表示不再需要进一步处理错误。
否则,errorHandler
应用组件将继续处理错误。
|
abstract public void processRequest()
|
abstract public function processRequest();
处理请求。 这实际是请处理工作已经完成的地方。 派生类应该重写此方法。
|
protected void registerCoreComponents()
|
protected function registerCoreComponents()
{
$components=array(
'coreMessages'=>array(
'class'=>'CPhpMessageSource',
'language'=>'en_us',
'basePath'=>YII_PATH.DIRECTORY_SEPARATOR.'messages',
),
'db'=>array(
'class'=>'CDbConnection',
),
'messages'=>array(
'class'=>'CPhpMessageSource',
),
'errorHandler'=>array(
'class'=>'CErrorHandler',
),
'securityManager'=>array(
'class'=>'CSecurityManager',
),
'statePersister'=>array(
'class'=>'CStatePersister',
),
'urlManager'=>array(
'class'=>'CUrlManager',
),
'request'=>array(
'class'=>'CHttpRequest',
),
'format'=>array(
'class'=>'CFormatter',
),
);
$this->setComponents($components);
}
注册核心应用组件。
参见
- setComponents
|
public void run()
|
public function run()
{
if($this->hasEventHandler('onBeginRequest'))
$this->onBeginRequest(new CEvent($this));
$this->processRequest();
if($this->hasEventHandler('onEndRequest'))
$this->onEndRequest(new CEvent($this));
}
运行应用程序。 此方法加载静态应用组件。 派生类通常重写此方法去做更多特定应用。 记住要调用父类实现,以使静态应用组件被加载。
|
public void saveGlobalState()
|
public function saveGlobalState()
{
if($this->_stateChanged)
{
$this->_stateChanged=false;
$this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
$this->getStatePersister()->save($this->_globalState);
}
}
保存全局状态数据到持久存储。
参见
- getStatePersister
|
public void setBasePath(string $path)
| ||
| $path | string | 应用程序根目录。 |
public function setBasePath($path)
{
if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
throw new CException(Yii::t('yii','Application base path "{path}" is not a valid directory.',
array('{path}'=>$path)));
}
设置应用程序根目录。 这个方法仅在构造函数时调用。
|
public void setExtensionPath(string $path)
| ||
| $path | string | 第三方扩展目录。 |
public function setExtensionPath($path)
{
if(($extensionPath=realpath($path))===false || !is_dir($extensionPath))
throw new CException(Yii::t('yii','Extension path "{path}" does not exist.',
array('{path}'=>$path)));
Yii::setPathOfAlias('ext',$extensionPath);
}
设置第三方扩展目录。
|
public void setGlobalState(string $key, mixed $value, mixed $defaultValue=NULL)
| ||
| $key | string | 要保存的值的名称 |
| $value | mixed | 被保存的全局值,它必须是序例化的。 |
| $defaultValue | mixed | 默认值。如果这个值与全局值上同,它将清除当前存储。 |
public function setGlobalState($key,$value,$defaultValue=null)
{
if($this->_globalState===null)
$this->loadGlobalState();
$changed=$this->_stateChanged;
if($value===$defaultValue)
{
if(isset($this->_globalState[$key]))
{
unset($this->_globalState[$key]);
$this->_stateChanged=true;
}
}
else if(!isset($this->_globalState[$key]) || $this->_globalState[$key]!==$value)
{
$this->_globalState[$key]=$value;
$this->_stateChanged=true;
}
if($this->_stateChanged!==$changed)
$this->attachEventHandler('onEndRequest',array($this,'saveGlobalState'));
}
设置一个全局值。
一个全局值,持续在是用户的sessions与requests之间。
确保这个值可以序例化和反序例化。
参见
- getGlobalState
|
public void setHomeUrl(string $value)
| ||
| $value | string | 设置主页URL |
public function setHomeUrl($value)
{
$this->_homeUrl=$value;
}
|
public void setId(string $id)
| ||
| $id | string | 应用程序唯一的标识符。 |
public function setId($id)
{
$this->_id=$id;
}
设置应用程序唯一的标识符。
|
public void setLanguage(string $language)
| ||
| $language | string | 用户的语言(例如,‘en_US’,‘zh_CN’)。 如果为null,将使用sourceLanguage。 |
public function setLanguage($language)
{
$this->_language=$language;
}
指定应用程序语言。
这个是应用程序显示最终用户的语言。
如果为null,它将使用source language。
除非你的应用程序需要支持多语言,
否则你应该总是设置language为null,以最大化应用程序的性能。
|
public void setLocaleDataPath(string $value)
| ||
| $value | string | 包含本地化数据的目录。 |
public function setLocaleDataPath($value)
{
CLocale::$dataPath=$value;
}
设置包含本地化数据的目录。
|
public void setRuntimePath(string $path)
| ||
| $path | string | 存储运行时文件目录。 |
public function setRuntimePath($path)
{
if(($runtimePath=realpath($path))===false || !is_dir($runtimePath) || !is_writable($runtimePath))
throw new CException(Yii::t('yii','Application runtime path "{path}" is not valid. Please make sure it is a directory writable by the Web server process.',
array('{path}'=>$path)));
$this->_runtimePath=$runtimePath;
}
设置存储运行时文件目录。
|
public void setTimeZone(string $value)
| ||
| $value | string | 应用程序使用的时区。 |
public function setTimeZone($value)
{
date_default_timezone_set($value);
}
设置应用程序所使用的时区。 这个一个简单的PHP函数date_default_timezone_set()的封装。
参见
- http://php.net/manual/en/function.date-default-timezone-set.php