CI framework commonly used classic Operation class summary (routing, pseudo Static, paging, session, verification code, etc.) _php instance

Source: Internet
Author: User
Tags autoload file upload codeigniter zend framework

In this paper, we summarize the common classical operation of CI framework. Share to everyone for your reference, specific as follows:

1. URIs in Super objects

Information about the resolution URL of the Ci_uri class

Use $this->uri directly to use its related properties

In the system/core/uri.php file

Some Common properties:

(1) Segment Get URL related information

$this->uri->segment (4);
Gets the
value of the fourth segment of PathInfo//in the URL

Entry file. php/controller/action/parameter 1/parameter 2/...

(2) by means of the formal parameters of the parameter

Need to set default values and order to note

Index.php/user/index/3/zhangsan

Public Function Index ($id =0, $name = ') {
  echo $id, $name;
}

Extension of the 2.CI controller

Under the Application/core/folder

To add your own extension controller

Class My_controller extends ci_controller{public
  function __construct () {
   parent::__construct
  }
}

Configure model Prefix

$config [' Subclass_prefix ']= ' my_ ';//Default value

3. Related operation of the model

File name all lowercase, class name first letter uppercase

Suggested class name plus _model suffix

To load the model in the controller:

Add in construct:

$this->load->model (' User_model ');
$this->user_model->get ();

Alias a model

$this->load->model (' User_model ', ' User ');
$this->user->get ();

Common functions in 4.url

(1) Help us build the controller

$this->load->helper (' url ');
Site_url (' controller/method ');

(2) The use of the image path

$this->load->helper (' url ');

upload/a.jpg "/>

Automatic loading can be configured in autoload.php

$autoload [' helper '] add URL

5. Routing and pseudo-static in CI

(1) Routing pseudo static

$router [' show/([\d]+) \.html ']= ' article/show/$1 ';
article/show/5.html => ARTICLE/SHOW/5;

(2) Hide the entry file


overridden by #开启apache的rewrite模块 #在根目录中放入. htaccess file
rewriteengine on
rewritecond%{request_filename}!-d
Rewritecond%{request_filename}!-f
Rewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]

6. Pagination in CI

The model operates
//loads the paging class file
$this->load->library (' pagination ');
$this->load->helper (URL);
Page links
$config [' base_url '] = Site_url (' user/test ');
Total number of records
$config [' total_rows '] = m;
Each page displays 10 data
$config [' per_page '] = ten;
Offset
$offset _limit = intval ($this->uri->segment (3));
$this->pagination->initialize ($config);
echo $this->pagination->create_links ();

Customization of buttons on pagination (note that configuration is done before initialization)

$config [' first_link '] = ' home ';
...
$config [' uri_segment '] =3;//paging data query offset

In which paragraph of the URL corresponds to the $offset above

The default is 3, otherwise you need to modify the corresponding value

7. Use of the session in CI

Load Session Library
$this->load->library (' Session ');

(1) Get system session

For example, to obtain the IP address of the client
$this->session->userdata (' ip_address ');

(2) Add 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) Flash out the data (after one time out of the expiration)

Add
$this->session->set_flashdata (' Item ', ' value ');
Get
$this->session->flashdata (' item ');

The URL of the page in the login data to return to the login can be recorded.

Note: One-time data, once read, will be automatically destroyed.

To ensure security, add in the config.php generated randomly encrypted string

$config [' Encryption_key ']= "FJKDSFFJKHJD#KJH";

Do you want to encrypt the cookie

$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 ');

Data uploaded by file

$filedata = $this->upload->data ();

9. Authentication code in CI

Generate 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 ' => ', '
  img_height ' => ', ' Expiration ' =>7200
);
$cap = Create_captcha ($vals);
echo $cap [' image '];
The number obtained by the verification code is placed in the session
Session_Start ();
$_session[' cap ' = $cap [' word '];

More interested in CodeIgniter related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", "thinkphp Introductory Course", " Thinkphp Common Methods Summary, "Zend Framework Introduction Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on CodeIgniter framework.

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.