zoukankan      html  css  js  c++  java
  • CI框架源码学习笔记1——index.php

    做php开发一年多了,陆陆续续用过tp/ci/yii框架,一直停留在只会使用的层面上,关于框架内部的结构实际上是不甚了解的。为了深入的学习,决定把CI框架的源码从头到尾的学习一下,

    主要因为CI框架工作中用的多,而且比较轻量级,所以选择分析它,用的版本是3.1.3版本,官网可下载。

    做php的都知道,项目的源头是index.php这个文件,所有的变化都是从它衍生出来,我们也先来看看这个文件。

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

    上面的这行代码很简单,定义了ENVIRONMENT这个常量,默认定义为'development'

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

    定义三个变量,没啥可说明的。

    // 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
        }

    判断系统目录是否存在,如果不存在给出提示

    // 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)

    再次定义几个常量,具体内容我写在注释里面

        // 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。

  • 相关阅读:
    虚拟机安装RHEL8.0.0
    给KVM添加新的磁盘
    RedHat7.4安装在个人电脑(笔记本)中安装遇到的问题总结
    shell编程-ssh免交互批量分发公钥脚本
    Error:Connection activation failed: No suitable device found for this connection 问题最新解决方案
    Linux下系统防火墙的发展历程和怎样学好防火墙(iptalbes和firewalld)
    Linux bash命令行常用快捷键(Xshell和secure CRT以及gnome-terminal)
    编写mysql多实例启动脚本
    RHEL7配置端口转发和地址伪装
    java中的事务,四大特性,并发造成的问题,隔离级别
  • 原文地址:https://www.cnblogs.com/isuifeng/p/6535151.html
Copyright © 2011-2022 走看看