Home Backend Development PHP Tutorial CI框架学习笔记(二) -入口文件index.php_php实例

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

Jun 07, 2016 pm 05:16 PM
ci framework

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


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

1. 设置应用程序环境

define('ENVIRONMENT', 'development');
Copy after login

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

default:   exit('The application environment is not set correctly.');
Copy after login

为什么一开始就要配置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';
}
Copy after login

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

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

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

$system_path = 'system';
$application_folder = 'application';
Copy after login

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

if (defined('STDIN'))
{
   chdir(dirname(__FILE__));
}
Copy after login

  这段代码是干嘛的呢?首先,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");
}
Copy after login

几个定义的常量(PATH结尾的常量表示目录路径,DIR结尾的变量表示目录名):
a. SELF(这里指index.php文件)
b. EXT(deprecated,废弃的,不必关注)
c. BASEPATH(system文件夹的路径)
d. FCPATH(前端控制器的路径)
e. SYSDIR(系统system目录名)
f. APPPATH(应用程序路径)
查看所有定义的常量的方法:

Print_r(get_defined_constants());
Copy after login

(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.'/');
}
Copy after login

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

require_once BASEPATH.'core/CodeIgniter.php';
Copy after login

  总结一下,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';
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

How to use CI framework in PHP How to use CI framework in PHP Jun 27, 2023 pm 04:51 PM

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

How to use CI4 framework in php? How to use CI4 framework in php? Jun 01, 2023 pm 02:40 PM

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

A guide to CI frameworks in PHP A guide to CI frameworks in PHP May 22, 2023 pm 07:10 PM

With the development of the Internet and its continuous integration into people's lives, the development of network applications has become more and more important. As a well-known programming language, PHP has become one of the preferred languages ​​for developing Internet applications. Developers can use numerous PHP frameworks to simplify the development process, one of the most popular is the CodeIgniter (CI) framework. CI is a powerful PHP web application framework. It has the characteristics of lightweight, easy to use, optimized performance, etc., allowing developers to quickly build

How to introduce css into ci framework How to introduce css into ci framework Dec 26, 2023 pm 05:20 PM

The steps to introduce CSS styles in the CI framework are as follows: 1. Prepare CSS files; 2. Store the CSS files in the appropriate location of the CI framework project; 3. In the pages that need to use CSS styles, introduce CSS through the HTML <link> tag File; 4. Use the CSS class or ID name in the HTML element to apply the corresponding style.

Detailed explanation of the steps to reference CSS styles in the CI framework Detailed explanation of the steps to reference CSS styles in the CI framework Jan 16, 2024 am 09:28 AM

Tutorial: Detailed steps for introducing CSS styles in the CI framework, specific code examples are required Introduction: Style is a crucial part of developing web applications. Use CSS (Cascading Style Sheets) to beautify web pages and provide a better user experience. When developing using the CodeIgniter (CI) framework, how to correctly introduce CSS styles is particularly important. This article will introduce the detailed steps of introducing CSS styles in the CI framework and provide you with specific code examples. Step 1: Create CSS File First,

Steps to introduce CSS styles to web pages using CI framework Steps to introduce CSS styles to web pages using CI framework Jan 16, 2024 am 09:20 AM

The steps for introducing CSS styles in the CI framework require specific code examples. The CI (CodeIgniter) framework is a popular PHP development framework that is widely used to build efficient web applications. When developing web applications, a beautiful user interface is an important consideration. Using CSS styles can optimize and personalize the web application interface, giving users a better experience. In a CI framework, introducing CSS styles usually requires the following steps, accompanied by specific code examples. step 1:

How to use CI6 framework in php? How to use CI6 framework in php? Jun 01, 2023 pm 11:10 PM

PHP is a very popular web development language, and CodeIgniter (CI) is a very popular PHP framework. CodeIgniter provides many useful functions and features, bringing great convenience to developers. In this article, we will explore how to use the CI6 framework. Installing CI6 Before you can start using CI6, you must first complete the installation process. You need to first download the CI6 compressed package from the CodeIgniter official website. Then, unzip this file and place it in

See all articles