目录
二、coreComponents方法,返回核心应用组件的配置
一、10个获取组件实例的方法
1. getDb方法,db组件
2. getLog方法,日志组件
3. getErrorHandler方法,错误处理组件
4. getCache方法,cache组件
5. getFormatter方法,formatter组件
6. getRequest方法,request组件
7. getResponse方法,response组件
8. getView方法,视图组件
9. getUrlManager方法,url管理组件
10. getI18n方法,国际化组件
11. getMailer方法,邮件组件
12. getAuthManager方法,授权管理组件
13. getAssetManager方法,资源管理组件
14. getSecurity方法,security组件
/**
* db组件
*/
public function getDb()
{
return $this->get('db');
}
/**
* 日志组件
*/
public function getLog()
{
return $this->get('log');
}
/**
* 错误处理组件
*/
public function getErrorHandler()
{
return $this->get('errorHandler');
}
/**
* 缓存组件
*/
public function getCache()
{
return $this->get('cache', false);
}
/**
* formatter组件
*/
public function getFormatter()
{
return $this->get('formatter');
}
/**
* 请求组件
*/
public function getRequest()
{
return $this->get('request');
}
/**
* 响应组件
*/
public function getResponse()
{
return $this->get('response');
}
/**
* 视图组件
*/
public function getView()
{
return $this->get('view');
}
/**
* url管理组件
*/
public function getUrlManager()
{
return $this->get('urlManager');
}
/**
* 国际化组件
*/
public function getI18n()
{
return $this->get('i18n');
}
/**
* 邮件组件
*/
public function getMailer()
{
return $this->get('mailer');
}
/**
* 授权管理组件
*/
public function getAuthManager()
{
return $this->get('authManager', false);
}
/**
* 资源管理组件
*/
public function getAssetManager()
{
return $this->get('assetManager');
}
/**
* security组件
*/
public function getSecurity()
{
return $this->get('security');
}
二、coreComponents方法,返回核心应用组件的配置
/**
* 返回核心应用程序组件的配置
*/
public function coreComponents()
{
return [
'log' => ['class' => 'yii\log\Dispatcher'],
'view' => ['class' => 'yii\web\View'],
'formatter' => ['class' => 'yii\i18n\Formatter'],
'i18n' => ['class' => 'yii\i18n\I18N'],
'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
'urlManager' => ['class' => 'yii\web\UrlManager'],
'assetManager' => ['class' => 'yii\web\AssetManager'],
'security' => ['class' => 'yii\base\Security'],
];
}
三、end方法,应用程序终止
/**
* 应用程序终止
*/
public function end($status = 0, $response = null)
{
//异常触发,尝试继续触发请求结束事件
if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
$this->state = self::STATE_AFTER_REQUEST;
$this->trigger(self::EVENT_AFTER_REQUEST);
}
//结束请求,发送响应
if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
$this->state = self::STATE_END;
$response = $response ?: $this->getResponse();
$response->send();
}
// 测试环境,抛出异常
if (YII_ENV_TEST) {
throw new ExitException($status);
}
exit($status);
}
四、setContainer方法,设置对象属性
/**
* 用配置设置对象属性
*/
public function setContainer($config)
{
Yii::configure(Yii::$container, $config);
}