zoukankan      html  css  js  c++  java
  • yii框架详解 之 CWebApplication 运行流程分析

    在 程序入口处,index.php 用一句 Yii::createWebApplication($config)->run();  开始了app的运行。

    那么,首先查看 CWebApplication 的 构造函数,如下:

    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']));
    Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
    $this->preinit();
    $this->initSystemHandlers();
    $this->registerCoreComponents();
    $this->configure($config);
    $this->attachBehaviors($this->behaviors);
    $this->preloadComponents();
    $this->init();
    }

    再来看下,run()函数的流程:

    public function run()
    {
    if($this->hasEventHandler('onBeginRequest'))
    $this->onBeginRequest(new CEvent($this));
    register_shutdown_function(array($this,'end'),0,false);
    $this->processRequest();
    if($this->hasEventHandler('onEndRequest'))
    $this->onEndRequest(new CEvent($this));
    }

    最后看框架是如何处理请求的:

    public function processRequest()
    {
    if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))
    {
    $route=$this->catchAllRequest[0];
    foreach(array_splice($this->catchAllRequest,1) as $name=>$value)
    $_GET[$name]=$value;
    }
    else
    $route=$this->getUrlManager()->parseUrl($this->getRequest());
    $this->runController($route);
    }


    由此可知,在 框架中的配置文件中,指定的 preload 组件,会在处理请求和路由分析之前执行,那么通过preload机制,我们能做的事就很多了,
    比如:
    想动态加载一些路由测略时,就可以自定义组件,先行于 processRequest进程,在自己的组件里 $this->getUrlManager()->addRules()

    值得一提的是,Yii里的组件是单例模式,先判断有没有已经存在,再去实例化组件,这使得在各个地方调用的组件延续性很好。





  • 相关阅读:
    linux下测试web访问及网络相关的命令
    linux下的数据备份工具rsync讲解
    Centos安装PHP PS:LAMP环境时,为少出错误,先安装一下编译环境
    Centos6.6安装MySQL5.6.24
    Centos6.6安装apache2.4
    bash_profile和bashrc区别
    Centos安装 Apache2.4提示 APR not found的解决办法
    ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)
    MySQL ibdata1文件迁移
    Linux启动/停止/重启Mysql数据库的方法
  • 原文地址:https://www.cnblogs.com/caryfang/p/4535885.html
Copyright © 2011-2022 走看看