Home > Article > Backend Development > CodeIgniter framework learning essay, codeigniter essay_PHP tutorial
---------------------------- -------------------------------------------------- ------------------
Codeigniter Framework
------------------------------------------------- ----------------------------------------
Lecturer: Zou Yiliang
Weibo: weibo.com/it266
------------------------------------------------- ----------------------------------------
Main content
Introduction to CI
An in-depth look at the MVC design pattern
Controllers and views in CI
Super objects in CI
Database access
AR model
------------------------------------------------- ----------------------------------------
What is CI?
CodeIgniter is a lightweight but powerful PHP framework
Based on the MVC design pattern, it provides a rich set of class libraries
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
(The latest version as of 2015.7.1 is 3.0.0 - author's note)
What are the characteristics?
You want a compact frame
You need great performance
You need broad compatibility with various PHP versions and configurations on standard hosts
CI 2.1.4 requires PHP5.1.6
You want a framework that requires almost 0 configuration
You want a framework that doesn’t require any commands
You want a framework that doesn’t have to adhere to restrictive coding rules
You don’t want to be forced to learn a template language (although you can choose your preferred template parser)
You don’t like complexity, you love simplicity
You need clear, complete documentation
Directory structure description
license.txt License Agreement
user_guide User Manual
syste Framework core file
application application directory
index.php entry file
------------------------------------------------- ----------------------------------------
MVC
1. Entry file
The only script file that the browser directly requests
2. Controller
Coordinate models and views
3. Model
Provide data and save data
4. View
Only responsible for displaying
Form...
5. Action
is a method in the controller, used to be requested by the browser
MVC in CI
Pathinfo is used to access the url
Entry file.php/controller/action
Application directory:
controllers controllers
models models
views views
The default controller is welcome
The default action is index
Controller
1. No need to add suffix
2. The file name should be all lowercase For example user.php
3. All controllers directly or indirectly inherit from the CI_Controller class
4. In the controller, the action (method) requirements are:
public
Cannot start with _
View
1. If you load the view in the controller
//Write the view name directly without the extension. If there is a subdirectory, write the directory name
2. In the view, use native PHP code directly
3. Recommended to use
=$item['name']?>
Super Object
Current controller object
provides many attributes:
$this->load
Instance of loader class system/core/loader.php
Methods provided by the loader class:
view() Loading view
vars() Assign variables to the view
database() loads the database operation object
model() Load model object
helper()
$this->uri
Is an instance of the CI_URI class system/core/URI.php
Methods provided by the CI_URI class:
segment() is used to obtain the parameters in the uri
Traditional: Entry file.php/controller/action/parameter1/value1/parameter2/value2
Entry file.php/controller/action/value1/value2
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
Methods provided by the CI_URI class:
$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, use $this directly to access the properties in the super object
Database access
Modify configuration file
application/config/database.php
Load the database access object into the attribute of the super object $this->db
$this->load->query($sql);//Return object
$res=$this->db->query($sql);//Return object
$res->result();//Returns an array, the array is an object one by one
$res->result_array();//Returns a two-dimensional array, which is an associative array
$res->row()//Return the first piece of data, which is directly an object
Parameter binding $sql="select * from blog_user where name=?";
$this->db->query($sql,$name);//if When there are multiple question marks, an index array
needs to be passed in
Table prefix
$db['default']['swap_pre'] = 'swap_';
The configuration is the same. In the code, just hard-code 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 will be automatically replaced with new_
Automatic loading of db
applicationconfigautoload.php
$autoload['libraries'] = array(database);
Not required: $this->load->database();