CI framework learning Notes (1)-Environment installation, basic terms and framework processes, ci learning notes. CI framework learning Notes (1)-Environment installation, basic terms, and framework processes. when ci learning notes first use the CI framework, they plan to write a series of notes for CI source code reading, unfortunately, 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...
- Topic Name </li>
If ($ news_item ['uid'] = 0) {// judge and obtain the main menu
Echo"
- ". $ News_item ['title'].'
';
Foreach ($ news as $ child_item): // second loop
If ($ news_item ['id'] = $ child_item ['uid']) {// judge and obtain the corresponding sub-menu
Echo"
- "." 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"
- ". $ News_item ['title'].'
';
Menu ($ news_item ['id']); // recursive call
Echo "</ul> </li> ";
}
Endforeach;
}
------ Call method ------------------------------
>
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.
Environment (1)-Environment installation, basic terms, and framework process. when I started using the ci framework, I planned to write a series of notes for CI source code reading. unfortunately...