CI framework learning notes (I)-Environment installation, basic terms and Framework processes, ci learning notes

Source: Internet
Author: User
Tags php foreach

CI framework learning notes (I)-Environment installation, basic terms and Framework processes, ci learning 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, 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.


Let's take a look at the php ci framework development.

The general method is ul nesting, that is, nesting sub-menu ul in ul-li in the main menu. Two levels of loops are required.
First, loop through the main menu, you must have fixed conditions to determine the main menu, such as the main menu uid = 0 or other...

<Ul style = "margin-top: 20px; border-top: dashed 1px #666666;">
<Li style = "font-weight: 600;"> topic name </li>
<? Php foreach ($ news as $ news_item): // loop once
If ($ news_item ['uid'] = 0) {// judge and obtain the main menu

Echo "<li>". $ news_item ['title']. '<ul> ';
Foreach ($ news as $ child_item): // second loop
If ($ news_item ['id'] = $ child_item ['uid']) {// judge and obtain the corresponding sub-menu
Echo "<li>". "ss". $ child_item ['title']. "</li> ";
}
Endforeach;
Echo "</ul> </li> ";
}

Endforeach;?>
</Ul>

Of course, this is only limited to two levels of menu, multi-level or infinitus, you can use function Recursion
Function menu ($ uid = 0) {// sets the default value starting from the main menu
Global $ news;
Foreach ($ news as $ news_item ):

If ($ news_item ['uid'] = $ uid ){

Echo "<li>". $ news_item ['title']. '<ul> ';
Menu ($ news_item ['id']); // recursive call

Echo "</ul> </li> ";
}

Endforeach;

}
------ Call method ------------------------------
<Ul style = "margin-top: 20px; border-top: dashed 1px #666666 ;"

Who has a ci framework Chinese manual?

Zend Framework takes a lot of time and is not suitable for quick learning,
Currently, there are many frameworks at home and abroad, such as speedphp, qeephp, cakephp, and TP.
According to the requirements of the landlord, only CI is available. I personally think it is quite good,
About CodeIgniter
CodeIgniter is an application development framework and toolkit for PHP website developers. She provides a rich set of standard libraries and simple interfaces and logical structures to help developers develop projects more quickly. CodeIgniter can reduce the amount of code writing and devote your energy to the creative development of projects.
CodeIgniter is developed by CEORickEllis of Ellislab. Its core framework is specially compiled for this program, while many other class libraries, auxiliary functions and subsystems come from the content management system ExpressionEngine compiled by RickEllis and PaulBurdick. Inspired by RubyonRails, we have created a PHP framework and introduced the concept of the Framework into the general consciousness of the network community.
She is a small but powerful PHP framework. As a simple and "elegant" toolkit, she can build functional Web applications for PHP programmers. If you are a developer who shares a host with others and is worried about the deadline the customer requires, and if you are tired of those stupid frameworks, CodeIgniter is what you need, if...
* You want a small framework.
* You need outstanding performance.
* You need to be widely compatible with various PHP versions and configurations (such as PHP4) on standard hosts ).
* You want a framework with almost zero configuration.
* You want a framework that does not require the use of command lines.
* You want a framework that does not require adherence to restrictive encoding rules.
* You are not interested in PEAR large-scale integrated class libraries.
* You do not want to be forced to learn a template language (although you can select the template parser you want ).
* You do not like complexity, but love simplicity.
* You need clear and complete documents.

The most important thing is that CI documents are simple, rich, and easy to understand. Haha
If you want to learn, you can go to CI China to see it. I don't need to post an address for you.


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.