--------------------------------------------------------------------------------------------------------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>