CodeIgniter 是一个小巧但功能强大的 PHP 框架,作为一个简单而“优雅”的工具包,它可以为 PHP 程序员建立功能完善的 Web 应用程序。如果你是一个使用共享主机,并且为客户所要求的期限而烦恼的开发人员,如果你已经厌倦了那些傻大笨粗的框架
那么 CodeIgniter 就是你所需要的,如果…
- 你想要一个小巧的框架。
- 你需要出色的性能。
- 你需要广泛兼容标准主机上的各种 PHP 版本和配置。
- 你想要一个几乎只需 0 配置的框架。
- 你想要一个不需使用命令行的框架。
- 你想要一个不需坚守限制性编码规则的框架。
- 你对 PEAR 这种大规模集成类库不感兴趣。
- 你不希望被迫学习一门模板语言(虽然可以选择你喜欢的模板解析器)。
- 你不喜欢复杂,热爱简单。
- 你需要清晰、完善的文档。
配置默认控制器
application/config/routes.php ---> route['default_controller']='你的控制器名称'
注意:新建控制器的时候有些名字是保留的,不能用的,查看有哪些:保留字
创建并载入视图文件


分配数据给视图

处理视图中分配的数据

辅助函数与其中的URL辅助函数


扩展自定义函数
在system/core/Common.php添加
seover wrap" notranslate="" print-yes="" crayon-os-pc="" crayon-font-verdana="">
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/**
* 自定义函数p()
* 打印数组
*/
if ( ! function_exists('p'))
{
function p($var)
{
|
|
function sucess($url,$msg){
header('Content-Type:text/html;charset=utf-8');
$url=site_url($url);
|
表单验证类的使用
1,载入验证类
$this->load->library('form_validation');
2,设置规则
$this->form_validation->set_rules('name值','标签名称','规则');
3,执行验证(返回bool值)
$this->form_validation->run();
实例:

4,表单验证辅助函数
载入验证辅助函数:
$this->load->helper('form');
使用验证辅助函数:
|
set_value('name')//保留原来输入的数据,防止输出错误导致内容丢失
set_radio();
form_error('name','','');
|
实例:
错误提示汉化:
1,下载汉化包放到application/language/
2,在application/config/config.php 修改$config['language']=’ch’ //ch是汉化包文件夹名称
使用公共表单验证策略(提高复用性,比如添加和编辑可以使用相同的验证规则)
1,建立application/config/form_validation.php文件:

2,直接使用配置文件验证规则

定义模型Model
新建文件:application/models/name_model.php,内容如下:
class Name_model extends CI_Model{ //注意,模型首字母要大写
public function add($data){
$this->db->insert(‘table’,$data);
}
}
准备数据,并使用该模型:
$data=array(
‘name’=>$this->input->post(‘name’);
);
$this->load->model(‘Name_model’);
$this->Name_model->add($data); //使用该模型里的方法
使用别名,简化输入:
$this->load->model(‘Name_model’,’n’);
$this->n->add($data); //使用该模型里的方法
输入类的使用
$this->input->post(‘name’); //返回布尔值,可以用来判断
$this->input->get(‘name’);
$this->input->server(‘HTTP_HOST’); //$_SERVER是一个超全局数组
防止跨站
配置文件:application/config/config.php
$config['global_xss_filtering']=TRUE //默认 FLASE
数据库配置
1,配置文件:application/config/database.php,内容为:

2,配置自动加载:application/config/autoload.php
修改:
$autoload['libraries']=array('database'); //默认是不开启的
使用AR类操作数据【在模型里操作,查询需return $data】
配置文件:application/config/database.php
配置项:$active_record=TRUE //默认开启
【模型】
增加:
public function add($data){
$this->db->insert(‘table’,$data);
}
查询:
public function list(){
$this->db->get(‘table’)->result_array(); //返回数组
//$this->db->get(‘table’)->result(); //返回对象
return $data;
}
public function edit_select($id){
$this->db->where(array(‘id’=>$id))->get(‘table’)->result_array();
return $data;
}
编辑:
public function edit_save($id,$data){
$this->db->update(‘table’,$data,array(‘id’=>$id));
}
删除:
public function del($id){
$this->db->delete(‘table’,array(‘id’=>$id));
}
【控制器】
使用构造函数
public function __construct(){
parent::__constrruct();
$this->load->model(‘Name_model’,’n’);
}
增加:
查询:
删除:
编辑:
public function edit(){
$id=$this->uri->segment(4); //是第4个URL片段
$this->load->model(‘Name_model’,’n’);
$data['data']=$this->n->edit($id);
$this->load->helper(‘form’);
$this->load->view(‘admin/edit.html’,$data);
}
开启调试模式
在当前函数使用: $this->output->enable_profiler(TRUE);