这篇文章主要介绍了CI框架常用经典操作类,结合实例形式总结分析了CI框架URL、路由、伪静态、分页、session、验证码等相关操作类与使用技巧,需要的朋友可以参考下
本文实例总结了CI框架常用经典操作类。分享给大家供大家参考,具体如下:
1. 超级对象中的URI
CI_URI类的解析url的相关信息
直接使用$this->uri可以使用它的相关属性
system/core/URI.php文件中
部分常用属性:
(1) 分段获取url相关信息
$this->uri->segment(4); //获取url中pathinfo //的第四段的值
入口文件.php/控制器/动作/参数1/参数2/...
(2) 通过方法中的形参传参
需要设默认值和顺序要注意
index.php/user/index/3/zhangsan
public function index($id=0,$name=''){ echo $id,$name; }
2.CI控制器的扩展
在application/core/文件夹下面
添加自己的扩展控制器
class MY_Controller extends CI_Controller{ public function __construct(){ parent::__construct } }
配置模型前缀
$config['subclass_prefix']='MY_';//默认值
3.模型的相关操作
文件名全小写,类名首字母大写
建议类名加上 _model后缀
在控制器中加载模型:
在construct中加入:
$this->load->model('User_model'); $this->User_model->get();
为模型起别名
$this->load->model('User_model','user'); $this->user->get();
4.url中的常用函数
(1)帮助我们生成控制器
$this->load->helper('url'); site_url('控制器/方法');
(2)图片路径的使用
$this->load->helper('url');
<img src="/static/imghwm/default1.png" data-src="<?php echo base_url();? alt="Analysis of commonly used operation classes in CI framework" >upload/a.jpg" class="lazy" />
可以在autoload.php中配置自动加载
$autoload['helper']加入url
5. CI中的路由与伪静态
(1) 路由伪静态
$router['show/([\d]+)\.html']='article/show/$1'; article/show/5.html => article/show/5;
(2) 隐藏入口文件
#开启apache的rewrite模块 #在根目录中放入.htaccess文件进行重写 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
6. CI中的分页
//模型中操作 //装载分页类文件 $this->load->library('pagination'); $this->load->helper(url); //分页链接 $config['base_url'] = site_url('user/test'); //总记录条数 $config['total_rows'] = 100; //每页显示10条数据 $config['per_page'] = 10; //偏移量 $offset_limit = intval($this->uri->segment(3)); $this->pagination->initialize($config); echo $this->pagination->create_links();
分页中按钮的定制(注意在初始化之前配置好)
$config['first_link'] = '首页'; ... $config['uri_segment'] =3;//分页数据查询偏移量
在url的哪一段上,对应上面的$offset
默认是3,否则需要修改对应值
7. CI 中session的使用
//加载session库 $this->load->library('session');
(1)获取系统session
//比如获取客户端的ip地址 $this->session->userdata('ip_address');
(2) 添加自定义session
//添加 $this->session->set_userdata('some_name', 'some_value'); //获取 $this->session->userdata('some_name'); //删除 $this->session->unset_userdata('some_name');
(3)闪出数据 (取出一次后失效)
//添加 $this->session->set_flashdata('item', 'value'); //获取 $this->session->flashdata('item');
登录数据中 返回登录前的那一个页面的url可以记录下来,
注意:一次性的数据,读取一次后会自动销毁。
为了确保安全,在config.php生成随机加密的字符串中加入
$config['encryption_key']="fjkdsffjkhjd#kjh";
是否要将cookie加密
$config['sess_encrypt_cookie'] =TRUE;
8. CI中的文件上传
<form action="<?php echo site_url('user/upload');?>" enctype="multipart/form-data"> <input type="file" name="pic"/> <input type="submit" value="submit"> </form>
上传处理:
$config['upload_path']="./upload"; $config['allowed_types']='gif|jpeg|jpg'; $this->load->library('upload',$config); $this->upload->do_upload('pic');
文件上传的数据
$filedata = $this->upload->data();
9. CI中的验证码
//生成验证码 $this->load->helper('captcha'); $this->load->helper('url'); $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'];
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use