二十七、逐行阅读Yii2.0.43源码_Yii框架文件yii\base\Application.php(5)

目录

一、10个获取组件实例的方法

 二、coreComponents方法,返回核心应用组件的配置

三、end方法,应用程序终止

 四、setContainer方法,设置对象属性


 

一、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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值