yii執行原理


應用執行流程:

瀏覽器向服務器發送 Http Request
    |
  控制器(protected/controllers)
    |
    |---> Action
            |
         創建模型 (Model)
            |
         檢查$_POST輸入
            |
         渲染視圖
            |
         render()第二個參數作為控制器與視圖接口參數
            |
            |----> View (protected/views)
                     |
                   使用$this訪問控制器的變量(包括layout, widget)



-----------------------------------------------------------------

視圖渲染流程:

render($view, $data, $return)
    |
beforeRender()
    |
渲染View文件,調用renderPartial(),要求處理輸出結果
    |
    |----> 根據$view得到viewFile文件名
                    |
           renderFile(),要求返回渲染結果,做下一步處理
                    |
                    |-----------> 獲取widget的數目
                                         |
                                  從Yii::app()獲得render
                                  CWebApplication::getViewRenderer
                                  查詢component['viewRenderer'],默認沒有配置
                                         |
                                  Then, 調用renderInternal()
                                         |
                                         |---------> require View文件,渲染,根據需要返回渲染結果
                                                          |
                                         |<---------------|
                                         |
                    |<-------------------|
                    |
               處理輸出結果processOutput()
                    |
               按照caller參數,返回輸出,而不是echo輸出
    |<--------------|
    |
渲染layout文件
    |

----------------------------------------------------------------------

加載控制器及其方法:

根據route信息,獲得當前控制器
      |
初始化當前控制器,CController::init(),默認為空
      |
執行當前控制器,CController::run()
      |
      |----> 創建action,為空則默認為index
                     |
             得到CInlineAction的實例
                     |
             用父對象執行beforeControllerAction:默認是CWebApplication,直接返回TRUE
                     |
                執行action
                     |----> 備份原來的action
                                 |
                            執行beforeAction()
                                 |
                            runWithParams()----> 實際上是執行CInlineAction->runWithParams()
                                                             |
                                                 在實例中,執行SiteController->actionIndex()
                                                             |
                                                 渲染頁面:render('index')
                                                             |
                                 |<--------------------------|
                                 |
                            執行afterAction()
                                 |
                            恢復原來action
                                 |
                     |<----------|
                     |
             用父對象執行afterControllerAction:默認是CWebApplication,為空
       |<------------|
     完成


----------------------------------------------------------------


應用執行流程:

index.php
    |
require_once($yii)
    |
    |------------->yii.php
                    |
                  require(YiiBase.php)
                    |
                    |---------------->YiiBase.php
                                        |
                                      Define YII_XXX global variable
                                        |
                                      Define Class YiiBase
                                        |
                                      Autload Class YiiBase (自動加載類機制)
                                        |
                                      require interface.php
                                        |
                    |<------------------|
                    |
                   define null Class Yii
                    |
    |<--------------|
    |
createWebApplication($config)---------->|
                                        |
                                      YiiBase::createApplication('CWebApplication',$config)
                                        |
                                      Create Instance of CWebApplication
                                        |
                                        |--------->CWebApplication
                                                      |
                                                   CApplication($config)構造函數
                                                      |
                                                      |------>|
                                                           setBasePath
                                                              |
                                                           set path alias
                                                              | 
                                                           preinit() 空方法
                                                              | 
                                                           initSystemHandlers()
                                                              | 
                                                           configure($config) 將配置文件信息保存到Application
                                                              |
                                                           attachBehaviors()
                                                              | 
                                                           preloadComponents() --> 裝載在configure($config)中配置需要preload的components
                                                              | 
                                                           init()                                                              | 
                                                      |<------|
                                                      |
                                        |<------------|
                                        |
    |<----------------------------------|
    |
app->run()
    |
    |---->CWebApplication::processRequest()
                      |
                      |----> CWebApplication::runController($route)
                                       |
                                       |---->createController($route)
                                                    |
                                                 如果$route是空,添加默認controller,對於CWebApplication的controller是"site"
                                                    |
                                                 Controller類是SiteController,require該類文件
                                                    |
                                                 如果該類是CController的子類,修改id[0]為大寫,創建該類的實例
                                                    |
                                                    |---->CSiteController
                                                              |
                                                          extends from Controller 這是客戶化控制器的基本類,存在於components下
                                                          定義了頁面的通用布局
                                                              |
                                                          使用CController構造函數創建對象CSiteController,具體初始化數據見yii-1.png
                                                              |
                                                    |<--------|
                                                備份$this->_controller
                                                $this->_controller = $controller
                                                    |
                                                 調用控制器類的init()方法,默認為空
                                                    |
                                                 調用控制器類的run()方法,默認為CController的run()
                                                    |
                                                    |---->createAction()
                                                              |
                                                           if($actionID==='') $actionID設置為$this->default ("index")
                                                              |
                                                              |Yes
                                                              |----> return CInlineAction($this, $actionID)
                                                              |No              |
                                                           從Map創建           |
                                                              |      執行當前類CInlineAction的父類CAction的構造函數
                                                              |      設置_controller和$id
                                                              |                |
                                                              |<---------------|
                                                              |
                                                              |
                                                           這里得到一個CAction的實例
                                                              |
                                                           $this->getModule()作為parent,為空則使用Yii::ap
                                                              |
                                                           $parent->beforeControllerAction() ??
                                                              | 
                                                            $this->runActionWithFilters($action,$this->filters());
                                                              | 
                                                            $parent->afterControllerAction($this,$action);
                                                    |<--------|
                                                    |
                                                  恢復原來的$oldController
                                       |<-----------| 
                                       |
                       |<--------------|
                       |
                  End of processRequest()
                       |
    |<-----------------|
    |
End of app->run()

------------------------------------------------------------


獲取控制器和方法的ID

轉載:http://code.dimilow.com/how-to-get-current-controller-name-and-action-name-in-yii/


How to get current controller name and action name in Yii

By Dimi Low on July 7th, 2010 (6 responses)

To get current controller name/id inside your controller, or view
$controllerId = Yii::app()->controller->id;
//or
$controllerId = $this->getId();

To get current action name/id being executed, if you are inside beforeAction() or afterAction(), use the received CAction argument
//inside beforeAction or afterAction
public function beforeAction($action)
{
$actionId = $action->id;
...
or just elsewhere inside your controller
$actionId = $this->getAction()->getId();

 

----------------------------------------------------

 

使用YiiMailMessage發送郵件:

安裝yii-mail到protected/extension下

配置protected/config/main.php,如下
'import' => array(
......
'application.extensions.yii-mail.*',
),

......
'components' => array(
'mail' => array(
'class' => 'application.extensions.yii-mail.YiiMail',
'transportType' => 'smtp', /// case sensitive!
'transportOptions' => array(
'host' => 'mail.syncomni.com',
'username' => 'huajian@syncomni.com',
// or email@googleappsdomain.com
'password' => 'Sitbwp4m2w',
// 'port' => '465',
// 'encryption' => 'ssl',
),
// 'viewPath' => 'application.views.mail',
'logging' => true,
'dryRun' => false
),
),
發送郵件
// 發送電子郵件
$message = new YiiMailMessage;
$message->setSubject($model->notice_subject);
$message->setBody($model->notice);
$message->setTo('kevin@syncomni.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);

 

 

 

[轉]http://www.cnblogs.com/jinhuawang76/tag/yii/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM