yii架構詳解 之 CWebApplication 運行流程分析

來源:互聯網
上載者:User

標籤:

在 程式入口處,index.php 用一句 Yii::createWebApplication($config)->run();  開始了app的運行。
那麼,首先查看 CWebApplication 的 建構函式,如下:
public function __construct($config=null){Yii::setApplication($this);// set basePath at early as possible to avoid troubleif(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裡的組件是單例模式,先判斷有沒有已經存在,再去執行個體化組件,這使得在各個地方調用的組件延續性很好。


來自為知筆記(Wiz)

yii架構詳解 之 CWebApplication 運行流程分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.