Home Backend Development PHP Tutorial Analysis of commonly used operation classes in CI framework

Analysis of commonly used operation classes in CI framework

Jun 14, 2018 pm 02:37 PM
ci framework

这篇文章主要介绍了CI框架常用经典操作类,结合实例形式总结分析了CI框架URL、路由、伪静态、分页、session、验证码等相关操作类与使用技巧,需要的朋友可以参考下

本文实例总结了CI框架常用经典操作类。分享给大家供大家参考,具体如下:

1. 超级对象中的URI

CI_URI类的解析url的相关信息

直接使用$this->uri可以使用它的相关属性

system/core/URI.php文件中

部分常用属性:

(1) 分段获取url相关信息

$this->uri->segment(4);
//获取url中pathinfo
//的第四段的值
Copy after login

入口文件.php/控制器/动作/参数1/参数2/...

(2) 通过方法中的形参传参

需要设默认值和顺序要注意

index.php/user/index/3/zhangsan

public function index($id=0,$name=''){
  echo $id,$name;
}
Copy after login

2.CI控制器的扩展

在application/core/文件夹下面

添加自己的扩展控制器

class MY_Controller extends CI_Controller{
  public function __construct(){
   parent::__construct
  }
}
Copy after login

配置模型前缀

$config['subclass_prefix']='MY_';//默认值
Copy after login

3.模型的相关操作

文件名全小写,类名首字母大写

建议类名加上 _model后缀

在控制器中加载模型:

在construct中加入:

$this->load->model('User_model');
$this->User_model->get();
Copy after login

为模型起别名

$this->load->model('User_model','user');
$this->user->get();
Copy after login

4.url中的常用函数

(1)帮助我们生成控制器

$this->load->helper('url');
site_url('控制器/方法');
Copy after login

(2)图片路径的使用

$this->load->helper('url');
Copy after login

<img src="<?php echo base_url();?>upload/a.jpg" />
Copy after login

可以在autoload.php中配置自动加载

$autoload['helper']加入url

5. CI中的路由与伪静态

(1) 路由伪静态

$router[&#39;show/([\d]+)\.html&#39;]=&#39;article/show/$1&#39;;
article/show/5.html => article/show/5;
Copy after login

(2) 隐藏入口文件

#开启apache的rewrite模块
#在根目录中放入.htaccess文件进行重写
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
Copy after login

6. CI中的分页

//模型中操作
//装载分页类文件
$this->load->library(&#39;pagination&#39;);
$this->load->helper(url);
//分页链接
$config[&#39;base_url&#39;] = site_url(&#39;user/test&#39;);
//总记录条数
$config[&#39;total_rows&#39;] = 100;
//每页显示10条数据
$config[&#39;per_page&#39;] = 10;
//偏移量
$offset_limit = intval($this->uri->segment(3));
$this->pagination->initialize($config);
echo $this->pagination->create_links();
Copy after login

分页中按钮的定制(注意在初始化之前配置好)

$config[&#39;first_link&#39;] = &#39;首页&#39;;
...
$config[&#39;uri_segment&#39;] =3;//分页数据查询偏移量
Copy after login

在url的哪一段上,对应上面的$offset

默认是3,否则需要修改对应值

7. CI 中session的使用

//加载session库
$this->load->library(&#39;session&#39;);
Copy after login

(1)获取系统session

//比如获取客户端的ip地址
$this->session->userdata(&#39;ip_address&#39;);
Copy after login

(2) 添加自定义session

//添加
$this->session->set_userdata(&#39;some_name&#39;, &#39;some_value&#39;);
//获取
$this->session->userdata(&#39;some_name&#39;);
//删除
$this->session->unset_userdata(&#39;some_name&#39;);
Copy after login

(3)闪出数据 (取出一次后失效)

//添加
$this->session->set_flashdata(&#39;item&#39;, &#39;value&#39;);
//获取
$this->session->flashdata(&#39;item&#39;);
Copy after login

登录数据中 返回登录前的那一个页面的url可以记录下来,

注意:一次性的数据,读取一次后会自动销毁。

为了确保安全,在config.php生成随机加密的字符串中加入

$config[&#39;encryption_key&#39;]="fjkdsffjkhjd#kjh";
Copy after login

是否要将cookie加密

$config[&#39;sess_encrypt_cookie&#39;] =TRUE;
Copy after login

8. CI中的文件上传

<form action="<?php echo site_url(&#39;user/upload&#39;);?>" enctype="multipart/form-data">
 <input type="file" name="pic"/>
 <input type="submit" value="submit">
</form>
Copy after login

上传处理:

$config[&#39;upload_path&#39;]="./upload";
$config[&#39;allowed_types&#39;]=&#39;gif|jpeg|jpg&#39;;
$this->load->library(&#39;upload&#39;,$config);
$this->upload->do_upload(&#39;pic&#39;);
Copy after login

文件上传的数据

$filedata = $this->upload->data();
Copy after login

9. CI中的验证码

//生成验证码
$this->load->helper('captcha');
$this->load->helper(&#39;url&#39;);
$vals = array(
  'word'=>rand(1000,9999),
  'img_path'=>'./captcha/',
  'img_url'=>base_url().'/captcha/'
  'img_width'=>'150',
  'img_height'=>'100',
  'expiration'=>7200
);
$cap = create_captcha($vals);
echo $cap['image'];
//将验证码获取的数字放在session中
session_start();
$_SESSION['cap'] = $cap['word'];
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于CI框架无限级分类和递归的实现

如何使用CodeIgniter开发实现支付宝接口调用

如何使用CI框架实现文件上传的优化以及多文件上传

The above is the detailed content of Analysis of commonly used operation classes in CI framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

With the development of network technology, PHP has become one of the important tools for Web development. One of the popular PHP frameworks - CodeIgniter (hereinafter referred to as CI) has also received more and more attention and use. Today, we will take a look at how to use the CI framework. 1. Install the CI framework First, we need to download the CI framework and install it. Download the latest version of the CI framework compressed package from CI's official website (https://codeigniter.com/). After the download is complete, unzip

How to use CI framework in PHP How to use CI framework in PHP Jun 27, 2023 pm 04:51 PM

PHP is a popular programming language that is widely used in web development. The CI (CodeIgniter) framework is one of the most popular frameworks in PHP. It provides a complete set of ready-made tools and function libraries, as well as some popular design patterns, allowing developers to develop Web applications more efficiently. This article will introduce the basic steps and methods of developing PHP applications using the CI framework. Understand the basic concepts and structures of the CI framework. Before using the CI framework, we need to understand some basic concepts and structures. Down

How to use CI4 framework in php? How to use CI4 framework in php? Jun 01, 2023 pm 02:40 PM

PHP is a widely used server-side scripting language, and CodeIgniter4 (CI4) is a popular PHP framework that provides a fast and excellent way to build web applications. In this article, we will get you started using the CI4 framework to develop outstanding web applications by walking you through how to use it. 1. Download and install CI4 First, you need to download it from the official website (https://codeigniter.com/downloa

A guide to CI frameworks in PHP A guide to CI frameworks in PHP May 22, 2023 pm 07:10 PM

With the development of the Internet and its continuous integration into people's lives, the development of network applications has become more and more important. As a well-known programming language, PHP has become one of the preferred languages ​​for developing Internet applications. Developers can use numerous PHP frameworks to simplify the development process, one of the most popular is the CodeIgniter (CI) framework. CI is a powerful PHP web application framework. It has the characteristics of lightweight, easy to use, optimized performance, etc., allowing developers to quickly build

How to introduce css into ci framework How to introduce css into ci framework Dec 26, 2023 pm 05:20 PM

The steps to introduce CSS styles in the CI framework are as follows: 1. Prepare CSS files; 2. Store the CSS files in the appropriate location of the CI framework project; 3. In the pages that need to use CSS styles, introduce CSS through the HTML <link> tag File; 4. Use the CSS class or ID name in the HTML element to apply the corresponding style.

Steps to introduce CSS styles to web pages using CI framework Steps to introduce CSS styles to web pages using CI framework Jan 16, 2024 am 09:20 AM

The steps for introducing CSS styles in the CI framework require specific code examples. The CI (CodeIgniter) framework is a popular PHP development framework that is widely used to build efficient web applications. When developing web applications, a beautiful user interface is an important consideration. Using CSS styles can optimize and personalize the web application interface, giving users a better experience. In a CI framework, introducing CSS styles usually requires the following steps, accompanied by specific code examples. step 1:

Detailed explanation of the steps to reference CSS styles in the CI framework Detailed explanation of the steps to reference CSS styles in the CI framework Jan 16, 2024 am 09:28 AM

Tutorial: Detailed steps for introducing CSS styles in the CI framework, specific code examples are required Introduction: Style is a crucial part of developing web applications. Use CSS (Cascading Style Sheets) to beautify web pages and provide a better user experience. When developing using the CodeIgniter (CI) framework, how to correctly introduce CSS styles is particularly important. This article will introduce the detailed steps of introducing CSS styles in the CI framework and provide you with specific code examples. Step 1: Create CSS File First,

How to use CI6 framework in php? How to use CI6 framework in php? Jun 01, 2023 pm 11:10 PM

PHP is a very popular web development language, and CodeIgniter (CI) is a very popular PHP framework. CodeIgniter provides many useful functions and features, bringing great convenience to developers. In this article, we will explore how to use the CI6 framework. Installing CI6 Before you can start using CI6, you must first complete the installation process. You need to first download the CI6 compressed package from the CodeIgniter official website. Then, unzip this file and place it in

See all articles