用户登录
用户注册

分享至

CI框架学习笔记(二) -入口文件index.php

  • 作者: 灬跪求粉色木耳灬
  • 来源: 51数据库
  • 2021-10-08

  上一节(ci框架学习笔记(一) - 环境安装、基本术语和框架流程)中,我们提到了ci框架的基本流程,这里再次贴出流程图,以备参考:


  作为ci框架的入口文件,源码阅读,自然由此开始。在源码阅读的过程中,我们并不会逐行进行解释,而只解释核心的功能和实现。

1. 设置应用程序环境

define('environment', 'development');

这里的development可以是任何你喜欢的环境名称(比如dev,再如test),相对应的,你要在下面的switch case代码块中,对设定的环境做相关的错误控制,否则,ci框架会认为你没有配置好相应的环境,从而退出进程并给出对应的错误信息:

default:   exit('the application environment is not set correctly.');

为什么一开始就要配置environment?这是因为,ci框架中很多组件都依赖于environment的配置,我们看一下system中,引用environment的地方:


  可以看到,很多组件都依赖于environment.例如,查看system/config/common.php, 这其中有一段引入配置文件的代码,是这样实现的:

if ( ! defined('environment') or ! file_exists($file_path = apppath.'config/'.environment.'/config.php'))
{
  $file_path = apppath.'config/config.php';
}

  在ci框架中,很多配置文件都是通过这种方式引入的,因此envrionment对于ci框架的正确运行时必须的,所以需要在开始的时候配置好environment。设置environment的一个好处是:可以很方便的切换系统的配置而不必修改系统代码。例如,在系统进入测试阶段时,database配置为测试的数据库,而在系统测试完毕时,database切换到线上的数据库。这好比是用一个开关控制了系统的环境切换,自然是非常方便的。

2.  配置系统目录和应用程序目录

  ci框架允许你将系统核心源码和应用程序代码分开放置,但是你必须设定好系统的system文件夹和application文件夹(同样,文件夹名字可以是任何合法的文件夹名称,而不一定使用'system'和'application'):

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

接下来,有这么一段代码:

if (defined('stdin'))
{
   chdir(dirname(__file__));
}

  这段代码是干嘛的呢?首先,stdin、stdout、stderr是php以 cli(command line interface)模式运行而定义的三个常量,这三个常量类似于shell的stdin,stdout,stdout,分别是php cli模式下的标准输入、标准输出和标准错误流。也就是说,这三行代码是为了保证命令行模式下,ci框架可以正常运行。关于php cli的更多细节可以参考:http://www.php-cli.com/

3. system目录的正确性验证和application目录验证

(1). system目录的正确性验证
  realpath返回的是目录或文件的绝对目录名(没有最后的/)

if (realpath($system_path) !== false)
{
  $system_path = realpath($system_path).'/';
}
$system_path = rtrim($system_path, '/').'/';
if ( ! is_dir($system_path))
{ 
  exit("xxxxxxxx");
}

几个定义的常量(path结尾的常量表示目录路径,dir结尾的变量表示目录名):
a. self(这里指index.php文件)
b. ext(deprecated,废弃的,不必关注)
c. basepath(system文件夹的路径)
d. fcpath(前端控制器的路径)
e. sysdir(系统system目录名)
f. apppath(应用程序路径)
查看所有定义的常量的方法:

print_r(get_defined_constants());

(2)application的目录验证。

代码较简单,不做过多的解释:

if (is_dir($application_folder))
{
  define('apppath', $application_folder.'/');
}
else
{
  if ( ! is_dir(basepath.$application_folder.'/'))
  {
    exit("your application folder path does not appear to be set correctly. please open the following file and correct this: ".self);
  }

  define('apppath', basepath.$application_folder.'/');
}

  入口文件的最后一行,引入codeigniter.php(也是下一步阅读的关键)。codeigniter.php被称为bootstrap file,也就是它是一个引导文件,是ci框架执行流程的核心文件。

require_once basepath.'core/codeigniter.php';

  总结一下,index.php并没有做太多复杂的工作,而是类似一个后勤,为ci框架的运行提供了一系列配置参数和正确性验证,而这些配置和验证,是ci框架能够正常运行的关键。

  最后,按照惯例,贴一下整个文件的源码(简化注释版):

<?php

define('environment', 'development');

if (defined('environment'))
{
  switch (environment)
  {
    case 'development':
      error_reporting(e_all);
    break;
  
    case 'testing':
    case 'production':
      error_reporting(0);
    break;

    default:
      exit('the application environment is not set correctly.');
  }
}

/*
 * system folder name
 */
$system_path = 'system';

/*
 * application folder name
 */
$application_folder = 'application';

/*
 * resolve the system path for increased reliability
 */
if (defined('stdin'))
{
  chdir(dirname(__file__));
}

if (realpath($system_path) !== false)
{
  $system_path = realpath($system_path).'/';
}

$system_path = rtrim($system_path, '/').'/';

if ( ! is_dir($system_path))
{
  exit("xxxxxxxx");
}

/*
 * set the main path constants
 */
// the name of this file
define('self', pathinfo(__file__, pathinfo_basename));

// this global constant is deprecataaed.
define('ext', '.php');

// path to the system folder
define('basepath', str_replace("\", "/", $system_path));

// path to the front controller (this file)
define('fcpath', str_replace(self, '', __file__));

// name of the "system folder"
define('sysdir', trim(strrchr(trim(basepath, '/'), '/'), '/'));

// the path to the "application" folder
if (is_dir($application_folder))
{
  define('apppath', $application_folder.'/');
}
else
{
  if ( ! is_dir(basepath.$application_folder.'/'))
  {
    exit("your application folder path does not appear to be set correctly. please open the following file and correct this: ".self);
  }

  define('apppath', basepath.$application_folder.'/');
}

require_once basepath.'core/codeigniter.php';
软件
前端设计
程序设计
Java相关