CodeIgniter framework learning and codeigniter
Bytes -----------------------------------------------------------------------------------------------
Codeigniter framework
Bytes -----------------------------------------------------------------------------------------------
INSTRUCTOR: Yan yiliang
Weibo: weibo.com/it266
Bytes -----------------------------------------------------------------------------------------------
Main Content
CI Introduction
In-depth MVC design model
Controller and view in CI
Super object in CI
Database Access
AR Model
Bytes -----------------------------------------------------------------------------------------------
What is CI?
CodeIgniter is a lightweight but powerful PHP framework.
Based on the MVC design pattern, a rich set of class libraries are provided.
Easy to learn, efficient and practical
Official Website
Www.codeigniter.com
Chinese website
Http://codeigniter.org.cn
Download the latest version
CodeIgniter_2.1.4.zip
(As of January, the latest version is 3.0.0-Note by the author)
What are the features?
You want a small framework
You need outstanding performance
You need to be widely compatible with various PHP versions and configurations on standard hosts
For CI 2.1.4, PHP5.1.6 is required.
You want a framework with almost zero Configuration
You want a framework without any command Operators
You want a framework that does not need to stick to restrictive coding rules.
You do not want to be forced to learn a template language (although you can select your preferred template parser)
You don't like complexity. You love simplicity.
You need clear and complete documentation
Directory structure description
License Agreement
User_guide User Manual
Javase framework Core File
Application directory
Index. php entry file
Bytes -----------------------------------------------------------------------------------------------
MVC
1. Entry File
The only script file directly requested by the browser
2. Controller
Coordinate models and views
3. Model
Provide data and save data
4. View
Only responsible for displaying
Form...
5. action
Is the method in the controller, used for browser requests
MVC in CI
The access url uses pathinfo.
Entry file. php/controller/Action
In the application directory:
Controllers Controller
Models Model
Views
The default controller is welcome.
The default action is index.
Controller
1. No suffix is required
2. All file names are in lowercase, for example, user. php.
3. All controllers directly or indirectly inherit from the CI_Controller class
4. Action (method) requirements in the controller:
Public
It cannot start _
View
1. Load the view in the Controller
// Write the view name directly without the extension. If there is a subdirectory, write the directory name.
2. directly use the native PHP code in the view
3. Recommended
<? Php foreach ($ list as $ item);?>
<? = $ Item ['name']?>
<? Php endforeach;?>
Super object
Current Controller object
Provides many attributes:
$ This-> load
Loader class instance system/core/loader. php
Methods provided by the loader class:
View () Load view
Vars () allocates variables to the view
Database () load database operation objects
Model () load model object
Helper ()
$ This-> uri
Is an instance of the CI_URI class system/core/URI. php
The CI_URI class provides the following methods:
Segment () is used to obtain parameters in the uri
Traditional: entry file. php/controller/Action/parameter 1/value 1/parameter 2/value 2
Entry file. php/controller/Action/value 1/value 2
Echo $ this-> segment (3); // value 1
Echo $ this-> segment (4); // value 2
// Index. php/controller/index/6
Public function index ($ p = 0) {echo $ p; // output 6
}
$ This-> input
Input class
Is an instance of the CI_URI class system/core/input. php
The CI_URI class provides the following methods:
$ This-> input-> post ('username'); // equivalent to $ _ POST ['username'];
$ This-> input-> server ('document _ root'); // equivalent to $ _ SERVER ['document _ root'];
$ This-> input-> server ('remote _ ADDR ');
In the view, you can directly use $ this to access attributes in the super object.
Database Access
Modify configuration file
Application/config/database. php
Load the database access object to the properties of the Super object $ this-> db
$ This-> load-> query ($ SQL); // return object
$ Res = $ this-> db-> query ($ SQL); // return object
$ Res-> result (); // returns an array, which contains objects one by one.
$ Res-> result_array (); // returns a two-dimensional array with an associated array
$ Res-> row () // returns the first data, directly an object
Parameter binding
$ SQL = "select * from blog_user where name =? ";
$ This-> db-> query ($ SQL, $ name); // If there are multiple question marks, You need to input an Index Array
Table prefix
$ Db ['default'] ['dbprefix'] = 'new _';
$ Db ['default'] ['SWAp _ pre'] = 'SWAp _';
The configuration is the same. In the code, you can hardcode the table prefix. If the project database table prefix changes in the future,
You only need to modify $ db ['default'] ['dbprefix'] = 'new _ '. swap _ in the Code is automatically replaced with new _
Automatic db Loading
Application \ config \ autoload. php
$ Autoload ['libraries'] = array (database );
Not required: $ this-> load-> database ();