CI框架代碼體會

本文主要記錄一些本身在瀏覽CI代碼時候的一些體會, 並不分析CI的使用。php

(http://codeigniter.org.cn/user_guide/toc.html  CI手冊地址)html

一 、 首先的一點就是, 已加載類在codeigniter, controller, model中的通用, 這使得CI的controller和model功能豐富。git

體如今Controller中的構造函數中:web

foreach (is_loaded() as $var => $class)
        {
            $this->$var =& load_class($class);   //將加載過的載入
        }

        $this->load =& load_class('Loader', 'core');

        $this->load->initialize();   // 加載autoload配置的庫類app

體如今model中:框架

function __get($key)
    {
        $CI =& get_instance();
        return $CI->$key;
    }ide

方法中函數

經過這樣的操做, 是的CI的controller, model 功能豐富。codeigniter

二、 CI容許擴展核心系統類:ui

在/system/core/common 中:

load_class 中有以下幾行:

foreach (array(APPPATH, BASEPATH) as $path)
		{
			if (file_exists($path.$directory.'/'.$class.'.php'))
			{
				$name = $prefix.$class;

				if (class_exists($name) === FALSE)
				{
					require($path.$directory.'/'.$class.'.php');
				}

				break;
			}
		}

		// Is the request a class extension?  If so we load it too
		if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
		{
			$name = config_item('subclass_prefix').$class;

			if (class_exists($name) === FALSE)
			{
				require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
			}
		}

對於Controller的重寫體如今(system/core/codeingiter):

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

這能夠知足本身的業務邏輯的修改, 例如能夠在本身擴展出來的controller中加一些權限控制的操做等等。

 

三、 Loader類也被controller加載了(依附在controller上的model也享受利益), 經過這個類,能夠實現各類功能庫的加載

 

四、 能夠掛載多個系統

根據業務需求可能須要開發多個系統, 能夠在index.php , 根據業務類型, 修改index.php

$applocation_folder參數

例如: 能夠在配置虛擬主機的時候, 設置service_name 等等 固然這只是一種思路

(待續)

對CI的common文件比較糾結的就是, 在整個框架代碼中, 忽然出來一個這個理的方法仍是有點讓人不知所措, 以爲應該在方法的前面加上一個common_之類的標記, 而後本身也能夠在裏面建立一個本身的同類文件, 在方法的前面標記上my_,, 可能會更加清晰一些

相關文章
相關標籤/搜索