Analysis of common operation classes in CI framework

Source: Internet
Author: User
This article mainly introduced the CI Framework Common classic operation class, combined with the example form summarizes the CI frame URL, routing, pseudo-static, paging, session, verification code and other related operations and use skills, the need for friends can refer to the next

This paper summarizes the common classical operation classes of CI framework. Share to everyone for your reference, as follows:

1. URIs in the Super object

Information about parsing URLs for the Ci_uri class

Using $this->uri directly can use its related properties

In the system/core/uri.php file

Some Common properties:

(1) segment to 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) through the parameter in the method of parameters

Need to set default values and order to be aware

Index.php/user/index/3/zhangsan

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

Expansion of the 2.CI controller

Under the Application/core/folder

Add your own extension controller

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

Configure model prefixes

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

3. Related operations of the model

File name all lowercase, class name capitalized

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) Use of 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) Route Pseudo-static

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

(2) Hidden entry file

#开启apache的rewrite模块 # 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. Pagination in CI

The model operates//loads paging class file $this->load->library (' pagination '); $this->load->helper (URL);//Paging link $config[' Base_ URL '] = Site_url (' user/test ');//total record bar $config[' total_rows '] = 100;//Show 10 data per page $config[' per_page '] = 10;//offset $offset_ Limit = Intval ($this->uri->segment (3)); $this->pagination->initialize ($config); Echo $this Pagination->create_links ();

Customization of buttons in pagination (note configuration before initialization)

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

Which section of the URL corresponds to the above $offset

Default is 3, otherwise you need to modify the corresponding value

7. Use of Session in CI

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

(1) Getting the system session

For example, get the IP address of the client $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) Flash data (once removed and then deactivated)

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

The URL of the page in the login data that is returned before login can be recorded,

Note: Once the data is read once, it will be destroyed automatically.

To ensure security, add a string that config.php generates a random encryption

$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 the file

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

9. The verification code in CI

Generate the Captcha $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 captcha in the SESSION session_start (); $_session[' cap '] = $cap [' word '];

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.