CI framework learning notes (1)-Environment installation, basic terms, and framework process _ php instance

Source: Internet
Author: User
This article is the first study Note of the CI framework. It mainly introduces the environment installation, basic terms, and framework process of the CI framework, which is very detailed, if you have any need, you can refer to the CI framework when you first use it. I plan to write a series of notes for reading the CI source code. Unfortunately, I have never taken any 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

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, which is also the main part of 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) routing http://www.kankanews.com/ICkengine/archives/70302.shtml to 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. It loads basic application configurations, 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 request views and sends them to the client. If cache is enabled, the view is cached for subsequent requests.

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: info-contact@alibabacloud.com 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.