zoukankan      html  css  js  c++  java
  • php优秀框架codeigniter学习系列——index.php

    程序流程图

      先来看看CI框架运行的程序流程图。

      从图中我们 看到,index.php作为唯一的入口文件,会初始化CI框架运行所需的基本资源。

      路由器(Routing)会根据http请求,确定如何处理;

      如果存在缓存文件(cache),会直接返回给浏览器,不用走下面的系统流程;

      在加载应用程序控制器(application controller)之前,会对http请求和用户请求数据进行必要的安全检查。

      控制器会加载模型,核心类库,辅助函数,需要用到的插件等请求所需要的资源。

      最后一步,渲染视图(viewer)并返回给浏览器,如果开启了缓存,会将视图先缓存起来,以用于以后的请求。 

    index.php

      在大致了解了程序流程之后,我们来详细逐段看下index.php中的内容。

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

    这里定义环境,有三个可以选择development,testing,production。根据定义的不同,其他的配置会有所改变。

     1 switch (ENVIRONMENT)
     2 {
     3     case 'development':
     4         error_reporting(-1);
     5         ini_set('display_errors', 1);
     6     break;
     7 
     8     case 'testing':
     9     case 'production':
    10         ini_set('display_errors', 0);
    11         if (version_compare(PHP_VERSION, '5.3', '>='))
    12         {
    13             error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
    14         }
    15         else
    16         {
    17             error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
    18         }
    19     break;
    20 
    21     default:
    22         header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    23         echo 'The application environment is not set correctly.';
    24         exit(1); // EXIT_ERROR
    25 }

    这里是根据ENVIRONMENT的值,定义报错的级别。

    1 $system_path = 'system';
    2 $application_folder = 'application';
    3 $view_folder = '';

    这里可以定系统程序文件夹,应用程序文件夹,视图文件夹的名称和位置。

    你还可以在这里定义一个默认的控制器来处理请求,而不通过常规的router,会覆盖掉router里面的配置,通常不建议这么做。

    你在这里还可以定义配置文件的参数,而不通过config.php文件配置,会覆盖掉config.php文件中的配置。

     1 /*
     2  * ---------------------------------------------------------------
     3  *  Resolve the system path for increased reliability
     4  * ---------------------------------------------------------------
     5  */
     6 
     7     // Set the current directory correctly for CLI requests
     8     if (defined('STDIN'))//如果定义有标准输入,这个值只有在CLI环境才会有 
     9     {
    10         chdir(dirname(__FILE__));//改变当前目录
    11     }
    12 
    13     if (($_temp = realpath($system_path)) !== FALSE)//realpath返回规范化的绝对路径名
    14     {
    15         $system_path = $_temp.DIRECTORY_SEPARATOR;
    16     }
    17     else
    18     {
    19         // Ensure there's a trailing slash
    20         $system_path = strtr(
    21             rtrim($system_path, '/\'),
    22             '/\',
    23             DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    24         ).DIRECTORY_SEPARATOR;
    25     }
    26 
    27     // Is the system path correct?
    28     if ( ! is_dir($system_path))
    29     {
    30         header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    31         echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME);
    32         exit(3); // EXIT_CONFIG
    33     }

       对系统目录路径做一些兼容处理。值得注意的是,realpath是一个神奇的函数,如果写的是相对路径,比如: $system_path=../system; 经过处理后会变成 /root/nginx/html/skin.qq.pinyin.cn/system ,非常好用。

     1     // The name of THIS file
     2     define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME));
     3 
     4     // Path to the system directory
     5     define('BASEPATH', $system_path);
     6 
     7     // Path to the front controller (this file) directory
     8     define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);
     9 
    10     // Name of the "system" directory
    11     define('SYSDIR', basename(BASEPATH));
    View Code

      接着,对 重要路径定义一些常量。

     1     // The path to the "application" directory
     2     if (is_dir($application_folder))
     3     {
     4         if (($_temp = realpath($application_folder)) !== FALSE)
     5         {
     6             $application_folder = $_temp;
     7         }
     8         else
     9         {
    10             $application_folder = strtr(
    11                 rtrim($application_folder, '/\'),
    12                 '/\',
    13                 DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    14             );
    15         }
    16     }
    17     elseif (is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR))
    18     {
    19         $application_folder = BASEPATH.strtr(
    20             trim($application_folder, '/\'),
    21             '/\',
    22             DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    23         );
    24     }
    25     else
    26     {
    27         header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    28         echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
    29         exit(3); // EXIT_CONFIG
    30     }
    31 
    32     define('APPPATH', $application_folder.DIRECTORY_SEPARATOR);
    33 
    34     // The path to the "views" directory
    35     if ( ! isset($view_folder[0]) && is_dir(APPPATH.'views'.DIRECTORY_SEPARATOR))
    36     {
    37         $view_folder = APPPATH.'views';
    38     }
    39     elseif (is_dir($view_folder))
    40     {
    41         if (($_temp = realpath($view_folder)) !== FALSE)
    42         {
    43             $view_folder = $_temp;
    44         }
    45         else
    46         {
    47             $view_folder = strtr(
    48                 rtrim($view_folder, '/\'),
    49                 '/\',
    50                 DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    51             );
    52         }
    53     }
    54     elseif (is_dir(APPPATH.$view_folder.DIRECTORY_SEPARATOR))
    55     {
    56         $view_folder = APPPATH.strtr(
    57             trim($view_folder, '/\'),
    58             '/\',
    59             DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR
    60         );
    61     }
    62     else
    63     {
    64         header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
    65         echo 'Your view folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF;
    66         exit(3); // EXIT_CONFIG
    67     }
    68 
    69     define('VIEWPATH', $view_folder.DIRECTORY_SEPARATOR);
    View Code

      同理系统目录,对应用程序目录,视图目录的路径做一些兼容处理。看起来似乎很笨拙,逻辑都是一样的,换了个目录又重写一遍。难免回想这样顶级的框架也会用这么笨的方法吗(因为也可以抽象成一个函数,把变量放进去)?可是这又的确是正确的办法。或许作者只是在该重复的时候重复,到了简洁的时候也一定会简洁的。能保证逻辑的准确性和完整性也是非常重要的。

    1 /*
    2  * --------------------------------------------------------------------
    3  * LOAD THE BOOTSTRAP FILE
    4  * --------------------------------------------------------------------
    5  *
    6  * And away we go...
    7  */
    8 require_once BASEPATH.'core/CodeIgniter.php';
    View Code

    配置完毕后,进入系统初始化的文件。

    总结

      从代码可以看到,作为框架唯一入口,index.php并没有做非常多的事情。他做的事情只是,定义程序环境,定义错误级别,定义相关重要目录的路径,并做一些兼容性的处理。接着转入系统初始化的文件。index.php做的事情是最最基本的工作,保证程序能找到框架对应的内容,和应用程序和系统文件的逻辑都关系不大。

     

    参考文档:

    应用程序流程图

     

  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Add Binary
    Subsets II
    Subsets
    Minimum Depth of Binary Tree
    node Cannot enqueue Quit after invoking quit.
    微擎快速修改数量实例(异步)
    destoon 分页
    ajax里面使用this方法
    微擎系统 微信支付 get_brand_wcpay_request:fail
  • 原文地址:https://www.cnblogs.com/Andres/p/8978895.html
Copyright © 2011-2022 走看看