--------------------------------------------------------------------------------------------------------php
载入视图html
$this->load->view('home/name'); //能够用子文件夹存储视图,默认视图文件以'.php'结尾mysql
载入多个视图sql
$data['title'] = 'chenwei'; //给视图添加动态数据数据库
$data['message'] = 'Your message';数组
$this->load->view('header', $data); //当一次性载入多个视图时,你只需在第一个视图传入数据便可(header视图显示title, content视图显示message)浏览器
$this->load->view('menu');服务器
$this->load->view('content');app
$this->load->view('footer');ide
使用对象的例子:
$data = new Someclass();
$this->load->view('blogview', $data);
视图文件中的变量
<title><?php echo $title; ?></title>
<div><?php echo $message; ?></div>
建立循环
class Blog extends CI_Controller{
function index()
{
$data['todo_list'] = array('clean house', 'call mom', 'run errands');
$data['title'] = 'my real title';
$data['heading'] = 'my real heading';
$this->load->view('blogview', $data);
}
}
<title><?php echo $title; ?></title>
<h1><?php echo $heading; ?></h1>
<ul>
<?php foreach($todo_list as $item):?>
<li><?php echo $item; ?></li>
<?php endforeach; ?>
</ul>
获取视图内容(赋值给一变量)
$buffer = $this->load->view('blogview', $data, true);
//view函数第三个可选参数能够改变函数的行为。若是将view第三个参数设置为true(布尔),则函数返回数据。view函数缺省行为是 false,将数据发送到浏览器。若是想返回数据,记得将它赋到一个变量中。
视图文件的PHP替代语法 =>
config/config.php中打开$config['rewrite_short_tags'],那么若是你的服务器不支持短标记,CodeIgniter将重写全部短标记。
注:若是你使用这个特性,若是在你的视图文件中发生 PHP 错误,则错误信息和行号将没法准确显示。相反,全部的错误将显示为 eval () 的错误。
正常的echo形式:<?php echo $variable; ?>
使用替代语法:<?=$variable?>
替代控制结构
<ul>
<?php foreach($todo_list as $item): ?>
<li><?=$item?></li>
<?php endforeach; ?>
</ul>
注:这里没有大括号。相反,结束大括号被替换成了 endforeach 。上面列出的每个控制结构也有类似的关闭语法:endif, endfor, endforeach 和 endwhile,而且在每一个结构之后注意不要使用分号(除了最后一个),用冒号!
<?php if($username == 'chenwei'): ?>
<h3>Hi chenwei.</h3>
<?php elseif($username == 'joe'): ?>
<h3>Hi Joe</h3>
<?php else: ?>
<h3>Hi unknow user</h3>
<?php endif; ?>
模型类文件均存放在 application/models 目录,固然也能够创建子目录,便于大型项目开发管理。
基本的模型类
1.类名首字母必须大写,其它字母小写,如 '表名_model.php',确保继承基本模型类CI_Model,文件名是模型类名的小写形式。
2.模型能够在控制器中被引用。
如:$this->load->model('User_model'); 或 $this->load->model('home/User_model');
模型一旦被载入 就可使用,默认状况下模型名称直接被引用做为对象名。
如:$this->User_model->function();
固然能够从新命名对象名,经过在加载模型函数中指定第二个参数来设定。
如:$this->load->model('User_model', 'fubar');
$this->fubar->function();
自动载入模型
若是须要特定模型在整个项目中起做用,可让CI在初始化时自动装载,经过在application/config/autoload.php文件的自动装载数组中添加该模型。
链接到数据库
模型被载入时不会自动链接数据库,如下方法可使你链接数据库,
1.标准方法链接数据库
2.把第三个参数设置为TRUE来使模型装载函数自动链接数据库
$this->load->model('User_model', '', TRUE);
3.手动设定第三个参数来载入你的自定义数据库配置
$config['hostname'] = 'localhost';
$config['username'] = 'root';
$config['password'] = 'root';
$config['database'] = 'test';
$config['dbdriver'] = 'mysql';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->load->model('User_model', '', $config);
//注:自动链接数据库和手动链接数据库时,消耗的内存状况同样。
完整示例:
class User_model extends CI_Model{
var $title = '';
var $connect = '';
var $data = '';
function __construct()
{
parent::__construct();
}
function get_last_ten_entries()
{
$query = $this->db->get('entries', 10);
return $query->result();
}
function insert_entry()
{
$this->title = $this->input->post('title'); //接收POST提交的数据,使用了input类
$this->content = $this->input->post('content');
$this->date = time();
$this->db->insert('entries', $this);
}
function update_entry()
{
$this->title = $this->input->post('title');
$this->content = $this->input->post('content');
$this->date = time();
$this->db->update('entries', $this, array('id'=>$this->input->post('id')));
}
}
//上面用到的函数是 Active Record 数据库函数
-----------------------------------------------------------------------------------------------------
控制器文件通常保存在application/controllers/ 文件夹:
默认URL路由配置 $config['uri_protocol'] = 'AUTO'; //默认即pathinfo模式,可选
注:类名必须大写字母开头,首字母小写属于无效写法。
基本的控制器类
class Blog extends CI_Controller{
public function __construct()
{
parent::__construct();
//构造函数并不能返回值,可是能够用来设置一些默认的功能。确保你的控制器扩展自父控制器类,以便它可以继承其全部的方法。
}
public function index()
{
echo 'Hello World!';
}
public function comments()
{
$this->load->view('comment');
}
}
//使用 example.com/index.php/blog/comments 来访问 comments方法
定义默认控制器
application/config/routes.php 中 $route['default_controller'] = 'Blog';
将控制器放入子文件夹
在application/controllers 目录下新建目录,放入控制器便可。注:若是你要使用某个子文件夹下的功能,就要保证 URI 的第一个片断是用于描述这个文件夹的。application/index.php/home/blog/comments/123
私有方法:
private function _test()
{
return $variable = 'aaa'; //即便不加修饰词private,只要方法名字前带下划线(_)作前缀,即为私有方法,没法经过URL访问。
}
保留的方法名称:
控制器类名不能为index, 如 class Index extends CI_Controller{},由于index为CI默认方法名,包含在保留字内,具体参考保留字。
从新定义方法的调用规则:
_remap();
处理输出:
_output(); 详细参考输出类。
----------------------------------------------------------------------------------------------
@黑眼诗人 <www.farwish.com>