博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Yii分析1:web程序入口(1)
阅读量:4080 次
发布时间:2019-05-25

本文共 4004 字,大约阅读时间需要 13 分钟。

以下分析基于Yii v1.0.6

 

Yii_PATH表示framework的路径

 

通常使用Yii框架的index.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 modedefined('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:

 

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

class CWebApplication extends CApplication{……}

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

 

Yii_PATH/base/CApplication.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();    }……}

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

 

Yii::setApplication($this);

 对应代码如下:

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,继续:

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,设置成员变量:

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

 之后:

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

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

public static function setPathOfAlias($alias,$path)    {        if(empty($path))            unset(self::$_aliases[$alias]);        else            self::$_aliases[$alias]=rtrim($path,'\\/');    }

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

$this->preinit();

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

protected function preinit(){}

 

接下篇:

 

 

转载地址:http://etsni.baihongyu.com/

你可能感兴趣的文章
ROS的安装(包含文字和视频教程,我的ROS安装教程以这篇为准)
查看>>
国内有个码云,gitee
查看>>
原来我之前一直用的APM固件....现在很多东西明白了。
查看>>
realsense-ros里里程计相关代码
查看>>
似乎写个ROS功能包并不难,你会订阅话题发布话题,加点逻辑处理,就可以写一些基础的ROS功能包了。
查看>>
if __name__ == ‘__main__‘:就是Python里的main函数,脚本从这里开始执行,如果没有main函数则从上到下顺序执行。
查看>>
PX4官方用户和开发手册的首页面是会给你选择英文和中文的
查看>>
网络协议栈我是不是可以这么理解,就是把你要发送的数据自动处理成TCPIP格式的消息发出去,这种底层的转换不需要你弄了。
查看>>
除了LwIP还有uIP
查看>>
《跟工程师学嵌入式开发》这本书最后的终极项目我反而觉得有说头
查看>>
博士的申请考核制
查看>>
那些硬件的初始化函数主要是在做些上什么?
查看>>
MAVLink学习之路05_MAVLink应用编程接口分析(也有讲STM32下的收发函数)
查看>>
找到了中文版的mavlink手册
查看>>
浅谈飞控开发的仿真功能
查看>>
我看他们不是弄了一个无人机降落的算法,我觉得你解决一个无人机抗磁干扰的也可以发论文啊。
查看>>
TBUS的无人机为了提升拉力是用的上下两个电机的无人机。
查看>>
我觉得在室内弄无人机开发装个防撞机架还是很有必要的,TBUS就做得很好。
查看>>
serial也是见到很多次了,似乎它就是一种串行通信协议
查看>>
TBUS的一些信息
查看>>