CI Framework Common classic operation class Summary (routing, pseudo-static, paging, session, Verification Code, etc.), ci
This example summarizes the common classic operation classes of the CI framework. 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
<form action="<?php echo site_url('user/upload');?>" enctype="multipart/form-data"> <input type="file" name="pic"/> <input type="submit" value="submit"></form>
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'];