热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Yii分析1:web程序入口(1)

Yii其实是YiiBase的helper,因此我们实际查看的是YiiBase::CreateWebApplication

以下分析基于Yii v1.0.6

 

Yii_PATH表示framework的路径

 

通常使用Yii框架的index.php程序如下:

PHP
// change the following paths if necessary $yii = dirname(__FILE__).'/protected/lib/Yii/framework/yii.php'; $cOnfig= dirname(__FILE__).'/protected/config/main.php'; // remove the following line when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); require_once $yii; $app = Yii::CreateWebApplication($config); $app->run();
1
2
3
4
5
6
7
8
9
10
// change the following paths if necessary
$yii    = dirname(__FILE__).'/protected/lib/Yii/framework/yii.php';
$cOnfig= dirname(__FILE__).'/protected/config/main.php';
 
// remove the following line when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);
 
require_once $yii;
$app = Yii::CreateWebApplication($config);
$app->run();


我们来看一下Yii::CreateWebApplication的过程:

 

Yii其实是YiiBase的helper,因此我们实际查看的是YiiBase::CreateWebApplication

 

Yii_PATH/YiiBase.php:

PHP
class YiiBase { …… public static function createWebApplication($cOnfig=null) { return new CWebApplication($config); } …… //自动类加载函数 public static function autoload($className) { // use include so that the error PHP file may appear if(isset(self::$_coreClasses[$className])) include(YII_PATH.self::$_coreClasses[$className]); else if(isset(self::$_classes[$className])) include(self::$_classes[$className]); else { include($className.'.php'); return class_exists($className,false) || interface_exists($className,false); } return true; } …… //核心类列表 private static $_coreClasses=array( 'CApplication' => '/base/CApplication.php', 'CApplicationComponent' => '/base/CApplicationComponent.php', 'CBehavior' => '/base/CBehavior.php', …… ); } //注册自动类加载函数 spl_autoload_register(array('YiiBase','autoload')); require(YII_PATH.'/base/interfaces.php');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    class YiiBase  
    {  
    ……  
        public static function createWebApplication($cOnfig=null)  
        {  
            return new CWebApplication($config);  
        }  
    ……  
        //自动类加载函数  
        public static function autoload($className)  
        {  
 
            // use include so that the error PHP file may appear  
            if(isset(self::$_coreClasses[$className]))  
                include(YII_PATH.self::$_coreClasses[$className]);  
            else if(isset(self::$_classes[$className]))  
                include(self::$_classes[$className]);  
            else  
            {  
                include($className.'.php');  
                return class_exists($className,false) || interface_exists($className,false);  
            }  
            return true;  
        }  
    ……  
 
        //核心类列表  
        private static $_coreClasses=array(  
            'CApplication' => '/base/CApplication.php',  
            'CApplicationComponent' => '/base/CApplicationComponent.php',  
            'CBehavior' => '/base/CBehavior.php',  
            ……  
        );  
 
    }  
    //注册自动类加载函数  
    spl_autoload_register(array('YiiBase','autoload'));  
    require(YII_PATH.'/base/interfaces.php');

 

这里返回的是一个CWebApplication的对象,

 

Yii_PATH/web/CWebApplication.php

PHP
class CWebApplication extends CApplication { …… }
1
2
3
4
    class CWebApplication extends CApplication  
    {  
    ……  
    }

CWebApplication继承自CApplication,没有自定义的constructor,因此我们继续查看CApplication的constructor:

 

Yii_PATH/base/CApplication.php

PHP
abstract class CApplication extends CModule { …… 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'])); $this->preinit(); $this->initSystemHandlers(); $this->registerCoreComponents(); $this->configure($config); $this->attachBehaviors($this->behaviors); $this->preloadComponents(); $this->init(); } …… }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    abstract class CApplication extends CModule  
    {  
    ……  
        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']));  
 
            $this->preinit();  
 
            $this->initSystemHandlers();  
            $this->registerCoreComponents();  
 
            $this->configure($config);  
            $this->attachBehaviors($this->behaviors);  
            $this->preloadComponents();  
 
            $this->init();  
        }  
    ……  
    }

这里,做了很多工作,我们来慢慢分析:

PHP
Yii::setApplication($this);
1
Yii::setApplication($this);

 

PHP
public static function setApplication($app) { if(self::$_app===null || $app===null) self::$_app=$app; else throw new CException(Yii::t('yii','Yii application can only be created once.')); }
1
2
3
4
5
6
7
    public static function setApplication($app)  
    {  
        if(self::$_app===null || $app===null)  
            self::$_app=$app;  
        else  
            throw new CException(Yii::t('yii','Yii application can only be created once.'));  
    }

这里只是set一下application的名称,ok,继续:

PHP
if(is_string($config)) $cOnfig=require($config); if(isset($config['basePath'])) { $this->setBasePath($config['basePath']); unset($config['basePath']); } else $this->setBasePath('protected');
1
2
3
4
5
6
7
8
9
    if(is_string($config))  
        $cOnfig=require($config);  
    if(isset($config['basePath']))  
    {  
        $this->setBasePath($config['basePath']);  
        unset($config['basePath']);  
    }  
    else  
        $this->setBasePath('protected');

这里主要是将createWebApplication时穿过来的配置文件require了一下,然后拿到配置项中的basePath,设置成员变量:

PHP
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))); }
1
2
3
4
5
6
    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)));  
    }

之后:

PHP
Yii::setPathOfAlias('application',$this->getBasePath()); Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
1
2
    Yii::setPathOfAlias('application',$this->getBasePath());  
    Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));

通过下面的函数设置路径的别名:

PHP
public static function setPathOfAlias($alias,$path) { if(emptyempty($path)) unset(self::$_aliases[$alias]); else self::$_aliases[$alias]=rtrim($path,'\\/'); }
1
2
3
4
5
6
7
    public static function setPathOfAlias($alias,$path)  
    {  
        if(emptyempty($path))  
            unset(self::$_aliases[$alias]);  
        else  
            self::$_aliases[$alias]=rtrim($path,'\\/');  
    }

保存在$_aliases数组中,接下来是一些初始化的工作(未完待续):

PHP
$this->preinit();
1
    $this->preinit();

调用的是Yii_PATH/base/CModule.php中的一个空函数,用于初始化模块(子类覆盖)

PHP
protected function preinit(){ }
1
2
protected function preinit(){  
}

推荐阅读
  • 本文详细介绍了暂估入库的会计分录处理方法,包括账务处理的具体步骤和注意事项。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 郑州大学在211高校中的地位与排名解析
    本文将详细解读郑州大学作为一所位于河南省的211和双一流B类高校,在全国211高校中的地位与排名,帮助高三学生更好地了解这所知名学府的实力与发展前景。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 优化ASM字节码操作:简化类转换与移除冗余指令
    本文探讨如何利用ASM框架进行字节码操作,以优化现有类的转换过程,简化复杂的转换逻辑,并移除不必要的加0操作。通过这些技术手段,可以显著提升代码性能和可维护性。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 电子元件封装库:三极管、MOS管及部分LDO(含3D模型)
    本资源汇集了常用的插件和贴片三极管、MOS管以及部分LDO的封装,涵盖TO和SOT系列。所有封装均配有高质量的3D模型,共计96种,满足日常设计需求。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • 小红书提高MCN机构入驻门槛,需缴纳20万元保证金
    近期,小红书对MCN机构的入驻要求进行了调整,明确要求MCN机构在入驻时需缴纳20万元人民币的保证金。此举旨在进一步规范平台内容生态,确保社区的真实性和用户体验。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 动物餐厅高效获取小鱼干攻略
    本文将介绍2023年动物餐厅中快速赚取小鱼干的有效方法,帮助玩家更轻松地积累资源。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
author-avatar
ID张蕾
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有