CI框架源碼學習筆記1——index.php

作php開發一年多了,陸陸續續用過tp/ci/yii框架,一直停留在只會使用的層面上,關於框架內部的結構其實是不甚瞭解的。爲了深刻的學習,決定把CI框架的源碼從頭至尾的學習一下,php

主要由於CI框架工做中用的多,並且比較輕量級,因此選擇分析它,用的版本是3.1.3版本,官網可下載。html

 

作php的都知道,項目的源頭是index.php這個文件,全部的變化都是從它衍生出來,咱們也先來看看這個文件。ubuntu

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

上面的這行代碼很簡單,定義了ENVIRONMENT這個常量,默認定義爲'development'app

switch (ENVIRONMENT)
{
    case 'development':
        error_reporting(-1);
        ini_set('display_errors', 1);
    break;

    case 'testing':
    case 'production':
        ini_set('display_errors', 0);
        if (version_compare(PHP_VERSION, '5.3', '>='))
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
        }
        else
        {
            error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
        }
    break;

    default:
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'The application environment is not set correctly.';
        exit(1); // EXIT_ERROR
}

根據當前的環境來設置php的一些報錯信息等級什麼的,分別對應開發/測試/生產環境框架

$system_path = 'system';
$application_folder = 'application';
$view_folder = '';

定義三個變量,沒啥可說明的。yii

// Set the current directory correctly for CLI requests
    if (defined('STDIN'))
    {
        chdir(dirname(__FILE__));
    }

英文註釋的意思是爲cli設置正確的目錄,網上搜了下,說是爲了保證命令行模式下,ci框架能夠正常運行學習

    if (($_temp = realpath($system_path)) !== FALSE)
    {
        $system_path = $_temp.DIRECTORY_SEPARATOR;
    }
    else
    {
        // Ensure there's a trailing slash
        $system_path = strtr(
            rtrim($system_path, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
        ).DIRECTORY_SEPARATOR;
    }

返回system目錄的絕對路徑,我用的是ubuntu,打印出來的內容是/var/www/html/ci/system測試

// Is the system path correct?
    if ( ! is_dir($system_path))
    {
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
        exit(3); // EXIT_CONFIG
    }

判斷系統目錄是否存在,若是不存在給出提示ui

// The name of THIS fileindex.php
    define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));//當前文件名稱(index.php)

    // Path to the system directory
    define('BASEPATH', $system_path);//system文件夾路徑(/var/www/html/ci/system/)

    // Path to the front controller (this file) directory
    define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);//當前文件路徑(/var/www/html/ci/)

    // Name of the "system" directory
    define('SYSDIR', basename(BASEPATH));//system目錄名稱(system)

再次定義幾個常量,具體內容我寫在註釋裏面this

    // The path to the "application" directory
    if (is_dir($application_folder))
    {
        if (($_temp = realpath($application_folder)) !== FALSE)
        {
            $application_folder = $_temp;
        }
        else
        {
            $application_folder = strtr(
                rtrim($application_folder, '/\\'),
                '/\\',
                DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
            );
        }
    }
    elseif (is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
    {
        $application_folder = BASEPATH.strtr(
            trim($application_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
        );
    }
    else
    {
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
        exit(3); // EXIT_CONFIG
    }

    define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);

定義application目錄,打印出來是/var/www/html/ci/application/

// The path to the "views" directory
    if ( ! isset($view_folder[0]) && is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR))
    {
        $view_folder = APPPATH.'views';
    }
    elseif (is_dir($view_folder))
    {
        if (($_temp = realpath($view_folder)) !== FALSE)
        {
            $view_folder = $_temp;
        }
        else
        {
            $view_folder = strtr(
                rtrim($view_folder, '/\\'),
                '/\\',
                DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
            );
        }
    }
    elseif (is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR))
    {
        $view_folder = APPPATH.strtr(
            trim($view_folder, '/\\'),
            '/\\',
            DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
        );
    }
    else
    {
        header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
        echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
        exit(3); // EXIT_CONFIG
    }

    define('VIEWPATH', $view_folder.DIRECTORY_SEPARATOR);

定義views目錄,/var/www/html/ci/application/views/

require_once BASEPATH.'core/CodeIgniter.php';

最後,引入新文件CodeIgniter.php

 

咱們能夠看到index.php文件其實沒有什麼太多的內容,它的主要目的是定義一些系統須要用到的常量,以及引入新的文件CodeIgniter.php。

相關文章
相關標籤/搜索