当前位置:Gxlcms > PHP教程 > CI框架中常用的操作类分析

CI框架中常用的操作类分析

时间:2021-07-01 10:21:17 帮助过:20人阅读

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

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

1. 超级对象中的URI

CI_URI类的解析url的相关信息

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

system/core/URI.php文件中

部分常用属性:

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

  1. $this->uri->segment(4);
  2. //获取url中pathinfo
  3. //的第四段的值

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

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

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

index.php/user/index/3/zhangsan

  1. public function index($id=0,$name=''){
  2. echo $id,$name;
  3. }

2.CI控制器的扩展

在application/core/文件夹下面

添加自己的扩展控制器

  1. class MY_Controller extends CI_Controller{
  2. public function __construct(){
  3. parent::__construct
  4. }
  5. }

配置模型前缀

  1. $config['subclass_prefix']='MY_';//默认值

3.模型的相关操作

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

建议类名加上 _model后缀

在控制器中加载模型:

在construct中加入:

  1. $this->load->model('User_model');
  2. $this->User_model->get();

为模型起别名

  1. $this->load->model('User_model','user');
  2. $this->user->get();

4.url中的常用函数

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

  1. $this->load->helper('url');
  2. site_url('控制器/方法');

(2)图片路径的使用

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

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

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

$autoload['helper']加入url

5. CI中的路由与伪静态

(1) 路由伪静态

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

(2) 隐藏入口文件

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

6. CI中的分页

  1. //模型中操作
  2. //装载分页类文件
  3. $this->load->library('pagination');
  4. $this->load->helper(url);
  5. //分页链接
  6. $config['base_url'] = site_url('user/test');
  7. //总记录条数
  8. $config['total_rows'] = 100;
  9. //每页显示10条数据
  10. $config['per_page'] = 10;
  11. //偏移量
  12. $offset_limit = intval($this->uri->segment(3));
  13. $this->pagination->initialize($config);
  14. echo $this->pagination->create_links();

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

  1. $config['first_link'] = '首页';
  2. ...
  3. $config['uri_segment'] =3;//分页数据查询偏移量

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

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

7. CI 中session的使用

  1. //加载session库
  2. $this->load->library('session');

(1)获取系统session

  1. //比如获取客户端的ip地址
  2. $this->session->userdata('ip_address');

(2) 添加自定义session

  1. //添加
  2. $this->session->set_userdata('some_name', 'some_value');
  3. //获取
  4. $this->session->userdata('some_name');
  5. //删除
  6. $this->session->unset_userdata('some_name');

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

  1. //添加
  2. $this->session->set_flashdata('item', 'value');
  3. //获取
  4. $this->session->flashdata('item');

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

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

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

  1. $config['encryption_key']="fjkdsffjkhjd#kjh";

是否要将cookie加密

  1. $config['sess_encrypt_cookie'] =TRUE;

8. CI中的文件上传

  1. <form action="<?php echo site_url('user/upload');?>" enctype="multipart/form-data">
  2. <input type="file" name="pic"/>
  3. <input type="submit" value="submit">
  4. </form>

上传处理:

  1. $config['upload_path']="./upload";
  2. $config['allowed_types']='gif|jpeg|jpg';
  3. $this->load->library('upload',$config);
  4. $this->upload->do_upload('pic');

文件上传的数据

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

9. CI中的验证码

  1. //生成验证码
  2. $this->load->helper('captcha');
  3. $this->load->helper('url');
  4. $vals = array(
  5. 'word'=>rand(1000,9999),
  6. 'img_path'=>'./captcha/',
  7. 'img_url'=>base_url().'/captcha/'
  8. 'img_width'=>'150',
  9. 'img_height'=>'100',
  10. 'expiration'=>7200
  11. );
  12. $cap = create_captcha($vals);
  13. echo $cap['image'];
  14. //将验证码获取的数字放在session中
  15. session_start();
  16. $_SESSION['cap'] = $cap['word'];

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

相关推荐:

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

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

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

以上就是CI框架中常用的操作类分析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行