CI Framework Learning Notes (i)-_php examples of environmental installation, basic terminology and framework processes

Source: Internet
Author: User
Tags smarty template

When the CI framework was first used, it was intended to write a series of CI-source reading notes, which, unfortunately, had not been acted on. Recently there are few projects, and finally there is some time to write something. Then prepare to record some of the previous notes and experience, on the one hand, the right to make a memo, on the other hand remind yourself: learn from learning and have a way out, forget the past means betrayal! Basic Terminology Description

Before the beginning of this article, it is necessary to make a simple explanation of the repeated occurrences of the term, if you are familiar with this part, you can skip it completely. The terms that recur and are mentioned in this article include:

Front-end controller (Front Controller):

A component that centralizes all the requests of a user, sending a user's request to a specific application controller. In the CI framework, refers to the frame's entry file index.php. The front-end controller itself is a design mode, which can be referred to "Java EE design mode" in detail.

Application Controller

The application controller is a specific controller that processes user request URLs, usually placing a set of related processing or requests on an application controller, such as: Usercontroller may contain user registration, authentication, personal information, personal pages, and other related operations.

Mvc

A cliché term is a code layering and organization pattern. Dividing the code into m (Model, business logic), V (view, views), C (Controller, Controller), etc., facilitates the separation of the business logic part and the view rendering part, and reduces the coupling of the code. Many of the frameworks in PHP are based on MVC patterns, such as Zf,yii,ci

Route routing

Although named route, this is not a router, but rather a process of intercepting a user's request and forwarding the request to a specific controller processing. The routing of different frames is different, but the rationale is the same.

Hook Hooks

The initial hook refers to "a link in message passing that monitors the delivery of messages and adds specific processing before message processing". The hook here refers to adding or changing the core functionality of the system without changing the core source of the framework, most typically including running a specific script before the controller is loaded or after the load completes.

CI Framework Configuration

The basic environment of this article: Linux x86_64 gnu/linux. Installed PHP (CGI) +nginx+mysql+redis (so many of the server-related configurations in this article are Nginx-oriented and temporarily ignoring Apache servers).

First download the CI framework of the source code, download the address: Http://codeigniter.org.cn/downloads Current stable version is 2.2.0. Extract the source code into the folder (assuming the/usr/nginx/html/ci directory).

Before configuring the CI framework, browse the directory structure of the framework:

which

Application: Directory of applications, all of your application code should be in this directory

index.php: The portal file for the framework

Static: Our own set of directories, put some css,image and JS static files (This can be completely put into the application directory, see a person's preferences)

System:ci framework system files, but also the main part of the source code reading

User_guide: User Guide, similar to offline user manual.

The CI framework needs fewer places to configure:

1. Configure Routes

The default application controller and 404 pages are configured in routes.php. Open the application/config/routes.php file and configure the following:

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

2. Configure Database database.php

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

The CI framework supports multiple data flow connections, and default is the currently Active_record connection, which specifies whether to enable arm (active record Model). Each configuration item is very concise and is no longer described here.

3. Remove index.php

Now to access your application, the URL should resemble this:

Test.xq.com/index.php/index
Test.xq.com/index.php/welcome

Note that each request will have a index.php segment. Removing index.php will make the URI more beautiful.

Open the test.xq.com.conf file you just added, 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 restarting the server, the way the URL is accessed now becomes:

Test.xq.com/index
Test.xq.com/welcome

Is it a lot simpler:D

4. Add. html Access suffix

There may also be people who like to add specific suffixes to URLs, such as. html suffixes make your application more like a series of static files. The configuration method is that, in application/config/config.php, the changes are configured as follows:

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

More configuration of the CI framework can refer to:

Let Nginx support. htaccess (This article does not mention the use of the. htaccess rewrite of the content, can refer to) http://www.php100.com/html/program/nginx/2013/0905/5537. Htmlci Framework Integration smarty, accustomed to using Smarty template engine of children's shoes can see http://www.kankanews.com/ICkengine/archives/70302.shtml configuration vhost

In order to facilitate access (compared to the way IP address access, domain name access has better memory), we can configure Vhost, configured by: Into the Nginx vhost directory, New configuration file (test.xq.com.conf in this article, in general, each of our vhost will be named after the domain name). Enter the following in the configuration file:

server {
  listen    ;
  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, and later when configuring the CI framework, 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  

where 10.130.130.130 should be the IP address of your server.

The CI framework can now be accessed through the domain name in the browser.

Framework Process

Before we end this article, we'll look at the basic process of the CI framework, which will be read throughout the source, so it's important to study it carefully. A flowchart on the user's manual for the CI Framework is referenced:

The basic execution process is as follows:

Index.php is a front-end controller that initializes all the resources required by the framework, loads the application's basic configuration, receives requests from all users, and, through Route routing user requests, if the cached file exists, it bypasses the usual execution order and sends it directly to the client. Security data filtering. This is before the application controller is loaded. The application controller loads the database driver, class library, business logic class, and possibly other resources, processing the user's request view to the client. If the cache is turned on, 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.