什么是MVC
mvc 是一種將應用程序的邏輯層和表示層分離開來的軟件方法
使用ci的默認控制器
- 可以在 application/routes.php 下設置 $route['default_controller'] = "";
- 隱藏 index.php 入口文件,首先開啟apache 的 Rewrite 重寫規(guī)則
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index.php|images|robots\\\\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
表單驗證類
- 在載入該表單視圖的控制器下的該方法下, 首先載入輔助函數(shù)
$this->load->helper('form');
- 在
<form action="控制器/方法名">
提交回來的方法下
載入$this->load->library('form_validation')
- 然后就是用
$this->form_validation->set_rules('表單元素的name 名',‘錯誤提示’,‘規(guī)則’)蔓涧;
- 然后使用
if($this->form_validation->run())
進行判斷,看是否符合規(guī)則 - 當然當我們提交表單錯誤后,會對頁面進行刷新,這時,我們就需要保留值以及提示錯誤信息了
在該表單視圖下的 input 下 使用<?php echo set_value('name名')?>
和在下面一行使用
<?php echo form_error('name名','<span>','</span>')?>
- CI提供了一個更加簡潔的方法給我們設置規(guī)則,在
application/config
下新建form_validation.php
<?php
$config = array(
'article'=>array(
'field' =>'name 名',
'label' => '錯誤提示',
'rules' =>'規(guī)則'
),
);
?>
- 最后只需要在
$this->form_validation->run('article');
自定義函數(shù)
- 可以在
system/core/common.php
下自定義函數(shù),它是全局加載的,在最下面加載
這里算是成功之后的跳轉
function success($url,$msg){
header("Content-Type:text/html;charset=utf-8");
$url = site_url($url);
echo "<script>alert('$msg');location.href='$url'</script>";
die;
}
- 調試模式
$this->output->enable_profiler(true)
;但是必須載入模板;$this->load->view()
可以隨便放
文件上傳
- 首先在控制器下,設置有關的配置
$config['upload_path']='image'; //與application 同級目錄下,上傳目錄
$config['allowed_types'] = 'jpg|gif|png|jpeg'; // 定義上傳類型,可以用 * 代替
$config['max_size'] = '10000';
$config['file_name'] = time().mt_rand(1000,9999);//使用時間戳定義文件名,不然的話相同名字的文件會被覆蓋
//載入上傳類,傳入配置
$this->load->library('upload',$config);
// 執(zhí)行上傳動作
$status = $this->upload->do_upload('filename'); //do_upload('') 里面的值為<input type='file' name ="filename"> name的值
// 提示錯誤,與上面的不一樣,類型不對或者路徑不對,或者是大小超過出現(xiàn)的錯誤信息
$wrong=$this->upload->display_errors()
if($wrong){
echo $wrong;
die;
}
//返回信息
$info = $this->upload->data();
可以嘗試打印一下
//print_r($info);
//縮略圖----------------
//同樣先設置一下配置,這里的數(shù)組可以隨便設置,可以設置成$config[],主要是因為不想與上面發(fā)生沖突
$thumb['sourse_image'] = $info['full_path'];
$thumb['create_thumb'] = FALSE;
$thumb['maintain_ratio'] = TRUE; // 像素比
$thumb['width'] = 200;
$thumb['height'] = 200;
//載入 縮略圖類
$this->load->library('image_lib',$thumb);
//執(zhí)行動作
$thumb_status=$this->image_lib->resize();
//同樣需要判斷一下是否縮略了
if(!$thumb_status){
echo "thumb fail";
die;
}
分頁
- 同樣載入視圖
$this->load->library('pagination')
- 寫配置
$perPage = 3 ; // 每頁顯示的條數(shù)
// 控制器/方法名
$config['base_url'] = site_url('article/index');
$config['total_rows'] = $this->db->count_all_results('artical');
//每頁顯示多少條
$config['per_page'] = $perPage;
$config['uri_segment'] = 3; //獲取到上面base_url
// 初始化pagination
$this->pagination->initialize($config);
//創(chuàng)建分頁
$data['links'] = $this->pagination->create_links();
$offset =$this->uri->segment(3);
$this->db->limit($perPage,$offset);
$this->load->model('artical');
$data['artical'] = $this->artical->artical_category();
$this->load->view('check_artical.html',$data);
- 另一實例
$this->load->library('pagination');
$config = array(
'base_url' => site_url().'/'.$this->uri->segment(1).'/'.$this->uri->segment(2),
'total_rows' => $this->db->count_all('articles'),
'per_page' => 14,
'num_links' => 5,
'first_link' => FALSE,
'last_link' => FALSE,
'full_tag_open' => "<ul class='pagination'>",//關閉標簽
'full_tag_close' => '</ul>',
'num_tag_open' => '<li>', //數(shù)字html
'num_tag_close' => '</li>', //當前頁html
'cur_tag_open' => "<li class='active'><a href='javascript:void(0),'>",
'cur_tag_close' => "</a></li>",
'next_tag_open' => '<li>', //上一頁下一頁html
'next_tag_close' => '</li>',
'prev_tag_open' => '<li>',
'prev_tag_close' => '</li>',
'prev_link' => "<i class='iconfont'></i>",
'next_link' => "<i class='iconfont'></i>"
);
$this->pagination->initialize($config);
$data=array(
'article' => $this->Article_model->get_article($id=FALSE,$config['per_page'],$this->uri->segment(3)), //$this->uri->segment(3) 就是上面設置的per_gage 參數(shù)
'article_nums' => $this->db->count_all('articles'),
'categoryes' =>$this->Categoryes_model->get_categoryes() //欄目
);
$this->load->view('article_list',$data);
子目錄下使用默認控制器
- 在根目錄下的index.php 添加代碼 $routing['directory'] = 'admin';
- 然后在application/config/routes.php 的默認控制器下添加 控制器名稱 ,比如
$route['default_controller'] = 'flashadmin';
CI框架的一些常量
我們可以在system/common.php,這個文件是用來加載基類庫和公共函數(shù)的,也就是說在里面創(chuàng)建函數(shù)可以直接使用
我們創(chuàng)建一個打印出ci框架的系統(tǒng)常量
function print_const(){
$const = get_defined_constants(TRUE);
p($const['user']);
}
function p($arr){
echo '<pre>';
print_r($arr);
echo '</pre>';
}
結果如下:
Array
(
[ENVIRONMENT] => development
[SELF] => index.php
[BASEPATH] => G:\xampp(1)\htdocs\wechat2016\system\
[FCPATH] => G:\xampp(1)\htdocs\wechat2016\
[SYSDIR] => system
[APPPATH] => G:\xampp(1)\htdocs\wechat2016\application\
[VIEWPATH] => G:\xampp(1)\htdocs\wechat2016\application\views\
[CI_VERSION] => 3.0.6
[SHOW_DEBUG_BACKTRACE] => 1
[FILE_READ_MODE] => 420
[FILE_WRITE_MODE] => 438
[DIR_READ_MODE] => 493
[DIR_WRITE_MODE] => 493
[FOPEN_READ] => rb
[FOPEN_READ_WRITE] => r+b
[FOPEN_WRITE_CREATE_DESTRUCTIVE] => wb
[FOPEN_READ_WRITE_CREATE_DESTRUCTIVE] => w+b
[FOPEN_WRITE_CREATE] => ab
[FOPEN_READ_WRITE_CREATE] => a+b
[FOPEN_WRITE_CREATE_STRICT] => xb
[FOPEN_READ_WRITE_CREATE_STRICT] => x+b
[EXIT_SUCCESS] => 0
[EXIT_ERROR] => 1
[EXIT_CONFIG] => 3
[EXIT_UNKNOWN_FILE] => 4
[EXIT_UNKNOWN_CLASS] => 5
[EXIT_UNKNOWN_METHOD] => 6
[EXIT_USER_INPUT] => 7
[EXIT_DATABASE] => 8
[EXIT__AUTO_MIN] => 9
[EXIT__AUTO_MAX] => 125
[MB_ENABLED] => 1
[ICONV_ENABLED] => 1
[UTF8_ENABLED] => 1
)