CI framework Source Code Reading Notes 1. ci framework source code notes

Source: Internet
Author: User

CI framework Source Code Reading Notes 1. ci framework source code notes
When I first started using the CI framework, I was planning to write a CI Source Code Reading Note series. Unfortunately, there was no action. There have been few projects recently, so I finally had some time to write something. So I want to record some of my previous notes and experiences. On the one hand, I have the right to make a memo. On the other hand, I always remind myself that learning and learning are the only way out. forgetting the past is a betrayal! Basic Terms

Before the beginning of this article, it is necessary to give a simple explanation of the terms that appear repeatedly in this article. If you are familiar with this part, you can skip it. The terms that have been repeatedly mentioned in this article include:

Front-end Controller ):

This component is used to centrally control all user requests and send user requests to specific application controllers. In the CI framework, it refers to the CodeIgniter class. The front-end controller itself is a design mode. For details, refer to the J2EE design mode.

Application Controller

The application controller is a specific controller that processes user request URLs. Generally, a group of related processing or requests are placed in an application controller. For example: userController may contain user registration, verification, personal information, personal pages, and other related operations.


  A conventional term is a code hierarchy and organization model. Code can be divided into M (Model, business logic), V (view, view), and C (Controller, Controller) layers to separate the business logic part and view rendering part, reduce code coupling. Currently, many PHP frameworks are based on MVC patterns, such as ZF, YII, and CI.


Although the name is Route, it is not a router, but a process of intercepting a user's request and forwarding the request to a specific Controller for processing. Different frameworks have different routes, but the basic principle is the same.


The initial Hook refers to "a part of message transmission, used to monitor message transmission and add specific processing before message processing ". The Hook here refers to adding or changing the core functions of the system without changing the core source code of the framework. The most typical situations include: run a specific script before or after the controller is loaded.

CI framework Configuration

The basic environment of this article: Linux x86_64 GNU/Linux. PHP (CGI) + Nginx + Mysql + redis is installed. (therefore, many server-related configurations in this article are dominated by Nginx, while Apache server is temporarily ignored ).

First download the CI framework source code, for: current stable version is 2.2.0. Decompress the source code to a folder (for example, the/usr/nginx/html/CI directory ).

Before configuring the CI framework, check the directory structure of the Framework:


Application: Application directory. All your application code should be in this directory.

Index. php: Framework entry file

Static: We create our own directory and place some static files such as CSS, image, and js (this can be placed under the application directory, depending on your preferences)

System: The system file of the CI framework is also the main part of the source code reading.

User_guide: User Guide, similar to offline user manual.

Few CI frameworks need to be configured:

1. Configure routes

In Routes. php, the default application controller and 404 page are configured. Open the application/config/routes. php file and configure it as follows:

$route['default_controller'] = "index";$route['404_override'] = '';

2. Configure database. php

If your application needs to provide dynamic content, the database is almost an essential configuration. Open the application/config/database. php file. The content of this file is as follows:

The CI framework supports multi-data stream connections, and the default is the current default connection. active_record is used to specify whether to enable ARM (Active Record Model ). Each configuration item is very concise and will not be described too much here.

3. Remove index. php

To access your application, the url should be similar to the following:

Note that each request carries an index. php segment. Removing index. php will make the URI more beautiful.

Open the just-added conf file and add the following configuration to the server:

if ($request_filename !~* /(favicon.ico|static|uploads|js|javascript|css|images|robots\.txt|index\.php|index\.html)){     rewrite ^/(.*)$ /index.php?$1 last;}

After the server is restarted, the URL access method is changed:

Is it more concise: D

4. extension. html access suffix

You can also customize the Suffix in the URL. For example, the suffix of .html makes your application more similar to a series of static files. The configuration method is as follows:

$config['url_suffix'] = '.html';

For more configurations of the CI framework, refer:

Configure Vhost

To facilitate access (Domain Name Access provides better memory than IP address access), we can configure vhost to enter the nginx vhost directory, create a configuration file ( conf. In general, each of our vhosts will be named by the domain name ). Enter the following content in the configuration file:

server {    listen       80;    server_name;    root         /usr/nginx/html/CI/;    access_log logs/xq_access_log main;    error_log  logs/testsq.log error;    charset GBK;    index index.php;    location ~ .*\.(php|php5)?$    {        include        fastcgi_params;        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;        fastcgi_pass;    }}

There are no other rewrite configurations in the Server. When configuring the CI framework later, we can add more configuration classes to support CI friendly URLs.

Open the local host file and add entries to the host: is the IP address of your server.

Now, you can access the CI framework through the domain name in the browser.

Framework process

Before the end of this article, let's take a look at the basic process of the CI framework, which runs through the source code. Therefore, it is necessary to carefully study it. The flowchart on the reference CI framework User Manual:


The basic execution process is as follows:

The next step is to start the CI source code reading Journey.

Who can explain the php code ci framework?

This is a method in the class.
Function: the browser uses the post request to send the shop_id to the server. This shop_id is the manufacture_id in the database. Based on this shop_id, the browser queries the database and outputs the records to the browser in json format.

I am looking for someone to help me read the background code. The ci-based framework does not really help new users.

How to create a website? Then we enter the background. Step 4: familiarize yourself with background operations! Then try to add an article by yourself! If you have other questions, you can ask me again... for example, the black and white network in the new century, Netease... what software do you need to learn when designing web pages? In addition, you need to use animation software, such as flash, and GIF animation software. If you have more GIF animation software, you can choose your preferred one. The background is followed by asp jsp php and ja for script code...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.