热门标签 | 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技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • Matplotlib,带有已保 ... [详细]
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在Python3中如何使用选择文件对话框的格式打开和保存图片的方法。通过使用tkinter库中的filedialog模块的asksaveasfilename和askopenfilename函数,可以方便地选择要打开或保存的图片文件,并进行相关操作。具体的代码示例和操作步骤也被提供。 ... [详细]
  • 本文描述了作者第一次参加比赛的经历和感受。作者是小学六年级时参加比赛的唯一选手,感到有些紧张。在比赛期间,作者与学长学姐一起用餐,在比赛题目中遇到了一些困难,但最终成功解决。作者还尝试了一款游戏,在回程的路上感到晕车。最终,作者以110分的成绩取得了省一会的资格,并坚定了继续学习的决心。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 关羽败走麦城时路过马超封地 马超为何没有出手救人
    对当年关羽败走麦城,恰好路过马超的封地,为啥马超不救他?很感兴趣的小伙伴们,趣历史小编带来详细的文章供大家参考。说到英雄好汉,便要提到一本名著了,没错,那就是《三国演义》。书中虽 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
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社区 版权所有