CodeIgniter.php 執行流程分析這是系統初始設定檔案1.定義CI版本2.定義CI分支 這裡我認為CI有兩個分支一個是Core ,另一個是Reactor。但是這裡具體的作用我還沒弄白。3.載入全域函數system/core/common.php4.載入架構常量 如果定義了ENVIRONMENT常量並且在APPPATH/cofig/下面有以ENVIRONMENT常量為名字的檔案夾並且裡面存在constants.php則載入這個constants.php如果沒有則直接載入APPPATH/cofig/下面的constants.php5.定義一個自訂的錯誤處理函數,讓我們用來記錄php錯誤記錄檔6.如果當前php版本>5.3 則關閉魔術轉義7.設定類首碼(在index.php中可以通過$assign_to_config[]來自訂配置項)一般的類首碼會在config檔案中被設定,這個類首碼可以讓CI知道application下的libraries檔案夾下哪些的類繼承了CI核心類的類,因為CI容許入口檔案index.php中覆蓋設定檔的配置項,所以程式開始執行之前我們需要知道類首碼是否被覆蓋,如果被覆蓋了,我們將在所有類載入之前設定它的值。 8.設定指令碼執行時間9.指令碼開始執行載入並執行個體化基準測試類別(類檔案:core/Benchmark.php)記下total_execution_time_start 、loading_time:_base_classes_start這兩個時間點10、如果可以載入並執行個體化鉤子類 (類檔案core/Hooks.php)我們就載入並執行個體化這需要在鉤子是在 application/config/config.php 中開啟並在 application/config/hooks.php檔案中定義當前的鉤子詳情:http://codeigniter.org.cn/user_guide/general/hooks.html11.載入並執行個體化配置類如果我們在index.php中手動的通過$assign_to_config[]來進行配置12、載入並執行個體化UTF-8類,這裡的順序是非常重要的UTF-8類必須跟在配置類的後面載入13、載入並執行個體化URI類14、載入並執行個體化router類,設定router15、載入並執行個體化輸出類16、判斷有沒有快取檔案如果有輸出17、為了防止xss和csrf攻擊載入並執行個體化security類18、載入並執行個體化輸入類19、載入並執行個體化語言套件20、載入並執行個體化控制器類get_instance() 這個函數用於執行個體化控制器類21、判斷是否存在子控制器類 如果存在載入22、通過控制器去載入本地應用程式下請求的控制器23、記錄loading_time:_base_classes_end時間點24、進行安全監測 此處有說明:所有的功能不管是應用程式控制器還是載入類可以通過URI調用,控制器function不能用底線開始先通過控制器獲得載入的類和方法當載入的類或者方法又或者方法不在CI_Controller這個類中的時候,顯示404頁面25、如果設定了pre_controller這個鉤子則調用26、記錄請求控制器的時間並且執行個體化請求的類27、如果設定了post_controller_constructor這個鉤子則調用28、調用請求的方法首先監測請求的類中是否有_remap方法,如果有調用如果沒有再判斷請求的類中是否有請求的方法如果沒有顯示404頁面最後調用請求的方法29、記錄控制器執行結束的時間30、如果設定了post_controller這個鉤子則調用31、將最終的輸出發送到瀏覽器32、如果設定了post_system鉤子則調用33、判斷CI_DB類是否存在還有$CI->db是否設定了值如果為真則關閉資料庫連接。 下面是檔案源碼:[php] $assign_to_config['subclass_prefix'])); } /* * ------------------------------------------------------ * Set a liberal 自由主義者 script execution 執行 time limit * ------------------------------------------------------ */ if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0) { @set_time_limit(300); } /* * ------------------------------------------------------ * Start the timer... tick tock tick tock... * ------------------------------------------------------ */ $BM =& load_class('Benchmark', 'core'); $BM->mark('total_execution_time_start'); $BM->mark('loading_time:_base_classes_start'); /* * ------------------------------------------------------ * Instantiate the hooks class * ------------------------------------------------------ */ $EXT =& load_class('Hooks', 'core'); /* * ------------------------------------------------------ * Is there a "pre_system" hook? * ------------------------------------------------------ */ $EXT->_call_hook('pre_system'); /* * ------------------------------------------------------ * Instantiate the config class * ------------------------------------------------------ */ $CFG =& load_class('Config', 'core'); // Do we have any manually 手動的 set config items in the index.php file? if (isset($assign_to_config)) { $CFG->_assign_to_config($assign_to_config); } /* * ------------------------------------------------------ * Instantiate the UTF-8 class * ------------------------------------------------------ * * Note: Order 命令順序 here is rather 寧可,當然 important as the UTF-8 * class needs to be used very early 早期的,提早 on, but it cannot * properly 適當的、正確的 determine 決定 if UTf-8 can be supported until 在。。。以前 * after the Config class is instantiated. * */ $UNI =& load_class('Utf8', 'core'); /* * ------------------------------------------------------ * Instantiate the URI class * ------------------------------------------------------ */ $URI =& load_class('URI', 'core'); /* * ------------------------------------------------------ * Instantiate the routing class and set the routing * ------------------------------------------------------ */ $RTR =& load_class('Router', 'core'); $RTR->_set_routing(); // Set any routing overrides that may exist in the main index file if (isset($routing)) { $RTR->_set_overrides($routing); } /* * ------------------------------------------------------ * Instantiate the output class * ------------------------------------------------------ */ $OUT =& load_class('Output', 'core'); /* * ------------------------------------------------------ * Is there a valid 有效 cache file? If so, we're done... * ------------------------------------------------------ */ if ($EXT->_call_hook('cache_override') === FALSE) { if ($OUT->_display_cache($CFG, $URI) == TRUE) { exit; } } /* * ----------------------------------------------------- * Load the security 安全 class for xss and csrf support * ----------------------------------------------------- */ $SEC =& load_class('Security', 'core'); /* * ------------------------------------------------------ * Load the Input class and sanitize 使。。。無害 globals * ------------------------------------------------------ */ $IN =& load_class('Input', 'core'); /* * ------------------------------------------------------ * Load the Language class * ------------------------------------------------------ */ $LANG =& load_class('Lang', 'core'); /* * ------------------------------------------------------ * Load the app controller and local controller * ------------------------------------------------------ * */ // Load the base controller class require BASEPATH.'core/Controller.php'; function &get_instance() { return CI_Controller::get_instance(); } if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php')) { require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'; } // Load the local application controller // Note: The Router class automatically 不經思索的、自動的 validates 確認 the controller // path using the router->_validate_request(). // If this include fails it means 手段、意思是 that 因為、那麼 the default controller in // the Routes.php fi.le is not resolving 解析 to something 非常 valid 有效地 if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php')) { show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.'); } include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'); // Set a mark point for benchmarking 管理標記 $BM->mark('loading_time:_base_classes_end'); /* * ------------------------------------------------------ * Security check * ------------------------------------------------------ * * None of the functions in the app controller or the * loader class can be called via the URI, nor 也不是、也沒有 can * controller functions that begin with an underscore 底線、強調、底線 * 所有的功能不管是應用程式控制器還是載入類可以通過URI調用,控制器function不能用底線開始 */ $class = $RTR->fetch_class(); $method = $RTR->fetch_method(); if ( ! class_exists($class) OR strncmp($method, '_', 1) == 0 OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller'))) ) { if ( ! emptyempty($RTR->routes['404_override'])) { $x = explode('/', $RTR->routes['404_override']); $class = $x[0]; $method = (isset($x[1]) ? $x[1] : 'index'); if ( ! class_exists($class)) { if ( ! file_exists(APPPATH.'controllers/'.$class.'.php')) { show_404("{$class}/{$method}"); } include_once(APPPATH.'controllers/'.$class.'.php'); } } else { show_404("{$class}/{$method}"); } } /* * ------------------------------------------------------ * Is there a "pre_controller" hook? * ------------------------------------------------------ */ $EXT->_call_hook('pre_controller'); /* * ------------------------------------------------------ * Instantiate the requested controller * ------------------------------------------------------ */ // Mark a start point so we can benchmark the controller $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start'); $CI = new $class(); /* * ------------------------------------------------------ * Is there a "post_controller_constructor" hook? * ------------------------------------------------------ */ $EXT->_call_hook('post_controller_constructor'); /* * ------------------------------------------------------ * Call the requested method * ------------------------------------------------------ */ // Is there a "remap" 重測圖、再交換 function? If so, we call it instead 更換、代替 if (method_exists($CI, '_remap')) { $CI->_remap($method, array_slice($URI->rsegments, 2)); } else { // is_callable() returns TRUE on some versions of PHP 5 for private and protected // methods, so we'll use this workaround 變通方案 for consistent behavior 一致的行為 if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI)))) { // Check and see if we are using a 404 override and use it. if ( ! emptyempty($RTR->routes['404_override'])) { $x = explode('/', $RTR->routes['404_override']); $class = $x[0]; $method = (isset($x[1]) ? $x[1] : 'index'); if ( ! class_exists($class)) { if ( ! file_exists(APPPATH.'controllers/'.$class.'.php')) { show_404("{$class}/{$method}"); } include_once(APPPATH.'controllers/'.$class.'.php'); unset($CI); $CI = new $class(); } } else { show_404("{$class}/{$method}"); } } // Call the requested method. // Any URI segments 片段 present 禮物、現在、目前 (besides 除了。。。之外 the class/function) // will be passed to the method for convenience 便利、方便 call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2)); } // Mark a benchmark end point $BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end'); /* * ------------------------------------------------------ * Is there a "post_controller" hook? * ------------------------------------------------------ */ $EXT->_call_hook('post_controller'); /* * ------------------------------------------------------ * Send the final rendered 提出、已渲染的 output to the browser * ------------------------------------------------------ */ if ($EXT->_call_hook('display_override') === FALSE) { $OUT->_display(); } /* * ------------------------------------------------------ * Is there a "post_system" hook? * ------------------------------------------------------ */ www.2cto.com $EXT->_call_hook('post_system'); /* * ------------------------------------------------------ * Close the DB connection if one exists * ------------------------------------------------------ */ if (class_exists('CI_DB') AND isset($CI->db)) { $CI->db->close(); } /* End of file CodeIgniter.php */ /* Location: ./system/core/CodeIgniter.php */
http://www.bkjia.com/PHPjc/477757.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477757.htmlTechArticleCodeIgniter.php 執行流程分析 這是系統初始設定檔案 1.定義CI版本 2.定義CI分支 這裡我認為CI有兩個分支一個是Core ,另一個是Reactor。但是這裡具...