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, the index. php. Front-end controller of the Framework entry file is a design mode. For details, see 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.
MVC
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.
Route route
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.
Hook
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: http://codeigniter.org.cn/downloads 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:
Where:
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:
test.xq.com/index.php/indextest.xq.com/index.php/welcome
Note that each request carries an index. php segment. Removing index. php will make the URI more beautiful.
Open the just-added test.xq.com. 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:
test.xq.com/indextest.xq.com/welcome
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:
- Let nginx support. htaccess (this article does not mention the use of. htaccess override content, you can refer to) http://www.php100.com/html/program/nginx/2013/0905/5537.html
- CI framework integrated smarty, used to use smarty template engine kids shoes can look at http://www.kankanews.com/ICkengine/archives/70302.shtml
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 (test.xq.com. 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 test.xq.com; 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 127.0.0.1:9000; }}
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:
10.130.130.130 test.xq.com
10.130.130.130 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:
- Index. php is a front-end controller that initializes all resources required by the framework, loads basic configurations of the application, receives requests from all users, and routes user requests through route.
- If the cached file exists, it will bypass the normal execution order and directly send it to the client.
- Security data filtering. This is before the application controller is loaded.
- The application controller loads database drivers, class libraries, business logic classes, and other resources to process user requests.
- The view is sent to the client. If cache is enabled, the view is cached for subsequent requests.
The next step is to start the CI source code reading Journey.
CI framework Source Code Reading Notes 1-Environment preparation, basic terms and Framework processes