This article mainly introduces common Classic operations of the CI framework, and summarizes and analyzes the operations and usage skills of the CI framework, such as URL, route, pseudo-static, paging, session, and verification code, for more information, see the examples in this article. We will share this with you for your reference. The details are as follows:
1. URI in the Super object
Information about the resolution url of the CI_URI class
You can directly use $ this-> uri to use its related attributes.
System/core/URI. php file
Some common attributes:
(1) obtain url-related information in segments
$ This-> uri-> segment (4); // obtain the value of the fourth segment of pathinfo // in the url
Entry file. php/controller/action/parameter 1/parameter 2 /...
(2) passing parameters through the form parameters in the method
Pay attention to the default value and sequence.
Index. php/user/index/3/zhangsan
public function index($id=0,$name=''){ echo $id,$name;}
2. expansion of CI controller
Under the application/core/folder
Add your own extended controller
class MY_Controller extends CI_Controller{ public function __construct(){ parent::__construct }}
Configure model prefix
$ Config ['subclass _ prefix'] = 'My _ '; // default value
3. model-related operations
All file names are in lowercase, and the first letter of the class name is in uppercase.
Suggested class name with _ model suffix
Load the model in the controller:
Add the following to construct:
$this->load->model('User_model');$this->User_model->get();
Alias for model
$this->load->model('User_model','user');$this->user->get();
4. common functions in URLs
(1) help us generate controllers
$ This-> load-> helper ('URL'); site_url ('Controller/method ');
(2) image path usage
$this->load->helper('url');
upload/a.jpg" />
You can configure automatic loading in autoload. php.
$ Autoload ['helper '] Add url
5. routing and pseudo-static in CI
(1) pseudo-static routing
$router['show/([\d]+)\.html']='article/show/$1';article/show/5.html => article/show/5;
(2) hide the entry file
# Enable the rewrite module of apache # put the. htaccess file in the root directory to rewrite RewriteEngine onRewriteCond % {REQUEST_FILENAME }! -DRewriteCond % {REQUEST_FILENAME }! -FRewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]
6. paging in CI
// Operate in the model // load the paging class file $ this-> load-> library ('pagination'); $ this-> load-> helper (url ); // pagination link $ config ['base _ url'] = site_url ('user/test'); // The total number of records $ config ['total _ rows '] = 100; // 10 pieces of data are displayed on each page. $ config ['per _ page'] = 10; // offset $ offset_limit = intval ($ this-> uri-> segment (3); $ this-> pagination-> initialize ($ config ); echo $ this-> pagination-> create_links ();
Button customization in the page (note that it is configured before initialization)
$ Config ['first _ link'] = 'homepage ';... $ config ['uri _ segment'] = 3; // the offset of querying paging data
Which segment of the url corresponds to the above $ offset
The default value is 3. Otherwise, the corresponding value needs to be modified.
7. Use of sessions in CI
// Load the session library $ this-> load-> library ('session ');
(1) obtain the system session
// For example, obtain the client ip address $ this-> session-> userdata ('IP _ address ');
(2) add a custom session
// Add $ this-> session-> set_userdata ('Some _ name', 'Some _ value '); // Get $ this-> session-> userdata ('Some _ name'); // delete $ this-> session-> unset_userdata ('Some _ name ');
(3) flashing data (invalid once Retrieved)
// Add $ this-> session-> set_flashdata ('item', 'value'); // Obtain $ this-> session-> flashdata ('ITEM ');
In the login data, the url of the page before logon can be recorded,
Note: One-time data is automatically destroyed once read.
To ensure security, add the random encryption string generated in config. php
$config['encryption_key']="fjkdsffjkhjd#kjh";
Whether to encrypt cookies
$config['sess_encrypt_cookie'] =TRUE;
8. file upload in CI
Upload processing:
$config['upload_path']="./upload";$config['allowed_types']='gif|jpeg|jpg';$this->load->library('upload',$config);$this->upload->do_upload('pic');
File upload data
$filedata = $this->upload->data();
9. the verification code in CI
// Generate the verification code $ this-> load-> helper ('captcha '); $ this-> load-> helper ('URL '); $ vals = array ('word' => rand (1000,9999), 'IMG _ path' => '. /captcha/', 'IMG _ url' => base_url (). '/captcha/''img _ width' => '000000', 'IMG _ height' => '000000', 'expiration' => 150 ); $ cap = create_captcha ($ vals); echo $ cap ['image']; // put the number obtained by the verification code in the session session_start (); $ _ SESSION ['Cap '] = $ cap ['word'];