PHP framework CodeIgniter framework learning Notes

Source: Internet
Author: User
CodeIgniter is a compact but powerful PHP Framework. As a simple and "elegant" toolkit, CodeIgniter can build functional Web applications for PHP programmers. If you are a developer who is using a shared host and is troubled by the deadline required by the customer, if you are tired of those silly frameworks
So CodeIgniter is what you need...
  • You want a small framework.
  • You need outstanding performance.
  • You need to be widely compatible with various PHP versions and configurations on standard hosts.
  • You want a framework with almost zero configuration.
  • You want a framework that does not need to use the command line.
  • 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 your preferred template parser ).
  • You don't like complexity. You love simplicity.
  • You need clear and complete documents.
Configure the default controller
Application/config/routes. php ---> route ['default _ controller'] = 'Your controller name'
Note:When creating a controller, some names are retained and cannot be used. Check the reserved words.
Create and load view files
Allocate data to views
Process the data allocated in the view
Auxiliary functions and their URL auxiliary functions
Extended user-defined functions
Add
Seover wrap "notranslate =" "print-yes =" "crayon-OS-pc =" "crayon-font-verdana =" ">
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* User-defined function p ()
* Print an array
*/
If (! Function_exists ('P '))
{
Function p ($ var)
    {
        
Print_r ($ var );
        
    }
}
 
1
2
3
4
5
Function sucess ($ url, $ msg ){
Header ('content-Type: text/html; charset = utf-8 ');
$ Url = site_url ($ url );
    
}
 
Use of form verification classes
1. Load the verification class
$ This-> load-> library ('form _ validation ');
2. Set rules
$ This-> form_validation-> set_rules ('name value', 'tag name', 'rule ');
3. Perform verification (return bool value)
$ This-> form_validation-> run ();
Instance:

4. Form verification auxiliary functions
Auxiliary function for loading verification: $ this-> load-> helper ('form ');
Use the verification helper function:
1
2
3
4
5
Set_value ('name') // retain the original input data to prevent content loss due to output errors
Set_select ();
Set_checkbox ();
Set_radio ();
Form_error ('name', '<span>', '</span> ');
Instance:
Incorrect prompt:
1. Download the Chinese package and place it in application/language/
2. In application/config. php, modify $ config ['language'] = 'ch' // ch to indicate the name of the Chinese package folder.
Use a public form verification policy(Improves reusability. For example, you can add and edit the same verification rules)
1. Create the application/config/form_validation.php file:

2. Directly use the configuration file to verify the rules
 
Define Model
Create a File: application/models/name_model.php. The content is as follows:
Class Name_model extends CI_Model {// note that the first letter of the model must be capitalized.
Public function add ($ data ){
$ This-> db-> insert ('table', $ data );
}
}
Prepare the data and use the model:
$ Data = array (
'Name' => $ this-> input-> post ('name ');
);
$ This-> load-> model ('name _ Model ');
$ This-> Name_model-> add ($ data); // use the method in this model
Use aliases to simplify input:
$ This-> load-> model ('name _ model', 'n ');
$ This-> n-> add ($ data); // use the method in this model
Use of input classes
$ This-> input-> post ('name'); // returns a boolean value, which can be used to determine
$ This-> input-> get ('name ');
$ This-> input-> server ('http _ host'); // $ _ SERVER is a super Global array.
Prevent cross-site
Configuration File: application/config. php
$ Config ['global _ xss_filtering '] = TRUE // Default FLASE
Database configuration
1. Configuration file: application/config/database. php. The content is:

2. Configure automatic loading: application/config/autoload. php
Modify: $ autoload ['libraries'] = array ('database'); // disabled by default
Use AR to operate data[Operate in the model. return $ data is required for query]
Configuration File: application/config/database. php
Configuration item: $ active_record = TRUE // enabled by default
[Model]
Added:
Public function add ($ data ){
$ This-> db-> insert ('table', $ data );
}
Query:
Public function list (){
$ This-> db-> get ('table')-> result_array (); // returns an array
// $ This-> db-> get ('table')-> result (); // return object
Return $ data;
}
Public function edit_select ($ id ){
$ This-> db-> where (array ('id' => $ id)-> get ('table')-> result_array ();
Return $ data;
}
Edit:
Public function edit_save ($ id, $ data ){
$ This-> db-> update ('table', $ data, array ('id' => $ id ));
}
Delete:
Public function del ($ id ){
$ This-> db-> delete ('table', array ('id' => $ id ));
}
Controller]
Use constructors
Public function _ construct (){
Parent: :__ constrruct ();
$ This-> load-> model ('name _ model', 'n ');
}
Added:
Query:
Delete:
Edit:
Public function edit (){
$ Id = $ this-> uri-> segment (4); // 4th URLs
$ This-> load-> model ('name _ model', 'n ');
$ Data ['data'] = $ this-> n-> edit ($ id );
$ This-> load-> helper ('form ');
$ This-> load-> view ('admin/edit.html ', $ data );
}
[View]
Upload xID:
<A href = "<? Php echo site_url ('admin/user/del/'. $ v ['id'])?> "> Edit </a>
Enable debugging mode
In the current function, use: $ this-> output-> enable_profiler (TRUE );

 

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.