Chinaunix首页 | 论坛 | 博客
  • 博客访问: 205056
  • 博文数量: 45
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 444
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:23
文章分类

全部博文(45)

文章存档

2017年(1)

2016年(14)

2014年(30)

我的朋友

分类: PHP

2014-03-28 11:44:31


目录结构说明
license.txt 许可协议
user_guide 用户手册
system 框架核心文件
application 应用目录
index.php 入口文件
------------------------------
MVC
1.入口文件
唯一一个让浏览器直接请求的脚本文件
2.控制器controller
协调模型和视图
3.模型model
提供数据,保存数据
4.视图view
只负责显示
表单...
5.动作action
是控制器中方法,用于被浏览器请求

CI中的MVC
访问url使用的是pathinfo

入口文件.php/控制器/动作

application目录中:
controllers 控制器 
models 模型
views 视图

默认控制器是welcome
默认动作是index

控制器
1.不需要加后缀
2.文件名全部小写  例如 user.php
3.所有的控制器,直接或间接继承自CI_Controller类
4.控制器中,对动作(方法)要求:
public
不能以_开头

视图
1.在控制器中如果加载视图
//直接写视图名字,不写扩展名,如果有子目录,则写上目录名
$this->load->view(视图);
可以多次调用$this->load->view(视图);
2.视图中,直接使用原生php代码
3.推荐使用




超级对象
当前的控制器对象
提供了很多属性:
$this->load
装载器类的实例system/core/Loader.php
装载器类提供方法:
view() 装载视图
vars() 分配变量到视图
database() 装载数据库操作对象 
model() 装载模型对象
helper()

$this->uri
是CI_URI类的实例 system/core/URI.php
CI_URI类提供方法:
segment(n) 用于获取url中的第n个参数(值)

传统的:入口文件.php/控制器/动作/参数1/值1/参数2/值2
入口文件.php/控制器/动作/值1/值2

echo $this->segment(3);//值1
echo $this->segment(4);//值2


//index.php/控制器/index/6
public function index($p=0){
echo $p;//输出6
}

$this->input
输入类
是CI_Input类的实例 system/core/Input.php
CI_Input类提供方法:
$this->input->post('username'); //$_POST['username']
$this->input->server('DOCUMENT_ROOT'); //$_SERVER['DOCUMENT_ROOT']

在视图中,直接用$this来访问超级对象中的属性


数据库访问

修改配置文件
application/config/database.php

将数据库访问对象,装载到超级对象的属性中 $this->db
$this->load->database();

$res=$this->db->query($sql);//返回对象
$res->result();//返回数组,数组中是一个一个的对象
$res->result_array();//返回二维数组,里面是关联数组
$res->row()//返回第一条数据,直接是一个对象

参数绑定
$sql="select * from blog_user where name=?";
$this->db->query($sql,$name);//如果有多个问号时,需要传入一个索引数组

表前缀
$db['default']['dbprefix'] = 'blog_';
$db['default']['swap_pre'] = 'blog_';

配置为一样,代码中,直接硬编码表前缀就行了,如果以后项目数据库表前缀发生变化,
只需要修改$db['default']['dbprefix'] = 'new_';代码中的blog_会自动替换为new_

db的自动加载
application/config/autoload.php
$autoload['libraries'] = array('database');
不需要:$this->load->database();

自增id
$this->db->insert_id();
受影响行数
$this->db->affected_rows();


Active Record
1.application/config/database.php
$active_record = TRUE;
2.application/config/autoload.php
$autoload['libraries'] = array('database');
3.在配置文件中,配置表前缀后,会自动添加


$res=$this->db->get('表名');//返回结果集对象
$res->result();

$bool=$this->db->insert('表名',关联数组);

$bool=$this->db->update('表名',关联数组,条件);

$bool=$this->db->delete('表名',条件);

//select id,name from tableName where id>=3 order by id desc limit 2,3
$res=$this->db->select('id,name')
->from('user')
->where('id >=',3)
->limit(3,2)//跳过2条,取出3条数据
->order_by('id desc ')
->get();

//显示最近一条SQL
echo $this->db->last_query();

//where
//$res=$this->db->where('name','mary')->get('user');
//$res=$this->db->where('name !=','mary')->get('user');
//$res=$this->db->where(array('name'=>'mary'))->get('user');
//$res=$this->db->where(array('name'=>'mary','id >'=>2))->get('user');


复杂的查询,请用$this->db->query($sql,$data);//使用问号绑定参数


扩展CI控制器
application/core/MY_Controller.php
控制器就要以继承自MY_Controller

application/config/config.php
$config['subclass_prefix'] = 'MY_';

模型
继承自CI_Model
在模型中,可以直接使用超级对象中的属性
文件名,全小写
类名首字母大写
建议使用_model作为后缀,防和控制器类名冲突


url相关函数
$this->load->helper('url');
//可以根需要配置自动加载
//application/config/autoload.php
//$autoload['helper'] = array('url');

site_url('控制器/方法')
base_url()


路由
application/config/routes.php
//默认控制器
$route['default_controller'] = "welcome";

//
$route['news/[\d]{6}/([\d]+)\.html']='article/show/$1';

隐藏入口文件
开始apache的rewrite模块,在httpd.conf文件中
LoadModule rewrite_module modules/mod_rewrite.so
重启apache
在入口文件同级目录中,放入一个.htaccess文件
内容如下:

  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]



分页
//装载类文件
$this->load->library('pagination');
$this->load->helper('url');
//每页显示10条数据
$page_size=10;

$config['base_url'] =site_url('user/test');
//一共有多少条数据
$config['total_rows'] = 100;
//每页显示条数
$config['per_page'] = $page_size; 
$config['first_link'] = '首页';
$config['next_link'] = '下一页';
$config['uri_segment']=3;//分页的数据查询偏移量在哪一段上

$this->pagination->initialize($config);

$offset=intval($this->uri->segment(3));//与$config['uri_segment']对应
$sql="select * from blog_user limit $offset, $page_size";
echo $sql;


$data['links']=$this->pagination->create_links();

$this->load->view('user/test',$data);



文件上传
1.手动创建好上传目录






//上传目录需要手工创建
$config['upload_path']='./uploads/';
//允许
$config['allowed_types']='gif|png|jpg|jpeg';
$config['max_size'] = '10000';
//生成新文件名
$config['file_name']=uniqid();
//装载文件上传类
$this->load->library('upload',$config);
$this->upload->do_upload('pic');

var_dump($this->upload->data());

//获取上传之后的数据
$data=$this->upload->data();
echo $data['file_name'];

验证码
//生成一个随机不重复的字符串作为加密用的key
//保存到application/config/config.php
//$config['encryption_key'] = 'adb8bf6d0ac4e17b42a80941582497a4';
//echo md5(uniqid());exit;
$this->load->library('session');
$user=array('id'=>3,'name'=>'jack');
//session_start();
//$_SESSION['user']=$user;
$this->session->set_userdata('user',$user);
//不在这这里获取刚放入的数据
//只有页在从新加载或跳转到别的url中,才能获取到

//一次性的数据,只能读取一次
$this->session->set_flashdata('test','aaaaaaaaaaaaaa');

}

public function show_session(){

$this->load->library('session');
//取CI session中的数据
$user=$this->session->userdata('user');
var_dump($user);

//下次刷新,就没有了
$test=$this->session->flashdata('test');
echo $test;

}

表单验证

$this->load->library('form_validation');


$this->form_validation->set_rules('name', '用户名', 'required');
$this->form_validation->set_rules('email', '邮箱', 'valid_email');

$bool=$this->form_validation->run();

if($bool){
//调用模型保存到数据库

}else{
//显示错误信息
$this->load->view('user/add');

}





name
','')?>


password

email






阅读(1570) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~