這篇文章給大家介紹CI框架常用經(jīng)典操作類有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

1. 超級(jí)對(duì)象中的URI
CI_URI類的解析url的相關(guān)信息
直接使用$this->uri可以使用它的相關(guān)屬性
system/core/URI.php文件中
部分常用屬性:
(1) 分段獲取url相關(guān)信息
$this->uri->segment(4); //獲取url中pathinfo //的第四段的值
入口文件.php/控制器/動(dòng)作/參數(shù)1/參數(shù)2/...
(2) 通過方法中的形參傳參
需要設(shè)默認(rèn)值和順序要注意
index.php/user/index/3/zhangsan
public function index($id=0,$name=''){
echo $id,$name;
}2.CI控制器的擴(kuò)展
在application/core/文件夾下面
添加自己的擴(kuò)展控制器
class MY_Controller extends CI_Controller{
public function __construct(){
parent::__construct
}
}配置模型前綴
$config['subclass_prefix']='MY_';//默認(rèn)值
3.模型的相關(guān)操作
文件名全小寫,類名首字母大寫
建議類名加上 _model后綴
在控制器中加載模型:
在construct中加入:
$this->load->model('User_model');
$this->User_model->get();為模型起別名
$this->load->model('User_model','user');
$this->user->get();4.url中的常用函數(shù)
(1)幫助我們生成控制器
$this->load->helper('url');
site_url('控制器/方法');(2)圖片路徑的使用
$this->load->helper('url');<img src="<?php echo base_url();?>upload/a.jpg" />
可以在autoload.php中配置自動(dòng)加載
$autoload['helper']加入url
5. CI中的路由與偽靜態(tài)
(1) 路由偽靜態(tài)
$router['show/([\d]+)\.html']='article/show/$1'; article/show/5.html => article/show/5;
(2) 隱藏入口文件
#開啟apache的rewrite模塊
#在根目錄中放入.htaccess文件進(jìn)行重寫
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');
//總記錄條數(shù)
$config['total_rows'] = 100;
//每頁顯示10條數(shù)據(jù)
$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;//分頁數(shù)據(jù)查詢偏移量
在url的哪一段上,對(duì)應(yīng)上面的$offset
默認(rèn)是3,否則需要修改對(duì)應(yīng)值
7. CI 中session的使用
//加載session庫
$this->load->library('session');(1)獲取系統(tǒng)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)閃出數(shù)據(jù) (取出一次后失效)
//添加
$this->session->set_flashdata('item', 'value');
//獲取
$this->session->flashdata('item');登錄數(shù)據(jù)中 返回登錄前的那一個(gè)頁面的url可以記錄下來,
注意:一次性的數(shù)據(jù),讀取一次后會(huì)自動(dòng)銷毀。
為了確保安全,在config.php生成隨機(jī)加密的字符串中加入
$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');文件上傳的數(shù)據(jù)
$filedata = $this->upload->data();
9. CI中的驗(yàn)證碼
//生成驗(yàn)證碼
$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'];
//將驗(yàn)證碼獲取的數(shù)字放在session中
session_start();
$_SESSION['cap'] = $cap['word'];關(guān)于CI框架常用經(jīng)典操作類有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁名稱:CI框架常用經(jīng)典操作類有哪些-創(chuàng)新互聯(lián)
地址分享:http://chinadenli.net/article2/dpgiic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)