本文轉(zhuǎn)載自:http://www.youarebug.com/forum.php?mod=viewthread&tid=105&page=1&extra=#pid142
目錄第一課 Hello World!第二課 初始MVC-->本帖第三課 URL及Ajax
今天的主要內(nèi)容是,使用CodeIgniter框架完整的MVC內(nèi)容來做一個簡單的計算器,通過這個計算器,讓大家能夠體會到我在第一節(jié)課中所介紹的標準的MVC框架與用戶交互的一個過程。下面,開始今天的課程。這行代碼的作用就是配置默認的控制器,為了讓大家能夠更好的學(xué)習(xí)到CI框架,這里,我們將這行代碼修改為
$route['default_controller'] = "calculate";
接下來,我們創(chuàng)建屬于我們自己的calculate控制器,在controllers文件夾下,新建一個php文件,文件名是calculate.php,然后書寫里面的代碼
<?php if ( ! defined('BASEPATH')) exit('No direct script accessallowed');/* * 計算控制器,類名首字母必須大寫,所有的控制器必須繼承自CI_Controller類 */class Calculate extends CI_Controller { // 構(gòu)造方法 function __construct() { parent::__construct(); } // 默認方法 function index() { // 加載calculate_view視圖 $this->load->view('calculate_view'); }}/* End of file calculate.php *//* Location: ./application/controllers/calculate.php */在上面的代碼中,我們可以注意到,我們的類名與文件名同名,但首字母是大寫的,這是CI的規(guī)則,必須這樣做,而且,所有的控制器都必須要繼承自CI_Controller類,另外,可以看到,我們在文件結(jié)束的地方只有兩行注釋,并沒有PHP的結(jié)束標簽“?>”,在這里不對此做過多解釋,只是CI推薦這樣寫,如果大家對這點感興趣,我會在后面的課程中講到。在上面的控制器當中加載了一個視圖,但是這個視圖現(xiàn)在還沒有,沒關(guān)系,現(xiàn)在我們就來動手寫這個視圖,在views文件夾下新建一個php文件,文件名為calculate_view.php,打開文件,書寫具體的代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>網(wǎng)頁計算器</title><style type="text/CSS">#calculators { margin: 10% auto; width:430px; border:1px solid #000;}</style></head><body><div id="calculators"> <form action="index.php/calculate/count" method="post"> <input type="text" name="num1" id="num1" /> <select name="Operate" id="operate"> <option value="+">+</option> <option value="-">-</option> <option value="x">x</option> <option value="÷">÷</option> </select> <input type="text" name="num2" id="num2" /> <input type="submit" value="計算" /> </form></div></body></html>
在上面的表單中,我們的提交路徑指向的其實是calculate類中的一個方法,這個方法的名字就是count,關(guān)于CI框架路徑的規(guī)則,我將在下一講中詳細介紹。接下來,我們來書寫controller中對應(yīng)的代碼,在calculate控制器中加入如下的函數(shù):
function count() { // 使用輸入類接收參數(shù) $num1 = $this->input->post('num1'); $op = $this->input->post('operate'); $num2 = $this->input->post('num2'); if (is_numeric($num1) && is_numeric($num2)) { // 如果兩個數(shù)輸入均為數(shù)字,則調(diào)用calculate_model模型下的count方法 $result = $this->calculate_model->count($num1, $num2, $op); }}
我們看到在控制器的代碼中,調(diào)用了calculate_model模型下面的count方法,但是在調(diào)用模型之前,控制器必須先要加載函數(shù),所以,需要在控制器的構(gòu)造函數(shù)中,加入如下代碼:
// 加載計算模型$this->load->model('calculate_model');
接下來,我們就按照控制器里面的調(diào)用,創(chuàng)建我們的calculate_model模型和count方法,所有的模型都是放在models文件夾下的,所以,我們需要在models文件夾下創(chuàng)建一個名為calculate_model.php的文件,創(chuàng)建好后,我們來書寫model端的代碼:
<?php/** * 計算模型,類名首字母必須大寫,所有的模型必須繼承自CI_Model類 */class Calculate_model extends CI_Model { function __construct() { parent::__construct(); } /* * 計算函數(shù) */ function count($num1, $num2, $op) { if ($op == "+") { return $num1 + $num2; }else if ($op == "-") { return $num1 - $num2; }else if ($op == "x") { return $num1 * $num2; }else if ($op == "÷" && $num2 != 0) { return $num1 / 1.0 / $num2; }else { return FALSE; } }}/* End of file calculate_model.php *//* Location: ./application/models/calculate_model.php */
現(xiàn)在,已經(jīng)進行到了模型將計算結(jié)果返回給了控制器,還剩最后一步,就構(gòu)成了一個標準的、完整的MVC框架執(zhí)行過程,那就是控制器再次加載視圖來顯示計算結(jié)果。我們需要在views文件夾下創(chuàng)建一個視圖(PHP文件),取名為result_view.php,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>網(wǎng)頁計算器</title><style type="text/css">#calculators { margin: 10% auto; width:430px; border:1px solid #000;}</style></head><body><div id="calculators"> <?php // 從控制器接收數(shù)據(jù)并對數(shù)據(jù)進行操作 if (is_numeric($num1) && is_numeric($num2) && $op && $result && $result != FALSE) { echo $num1." ".$op." ".$num2." = ".$result."<br />"; }else { echo "計算錯誤<br />"; } ?> <a href="/CI_02">返回首頁</a></div></body></html>
大家可以注意到,在這個視圖中,出現(xiàn)了PHP代碼,而且出現(xiàn)了一些變量,這些變量是哪來的呢?這就是今天要講到的另一個重點,給視圖添加動態(tài)數(shù)據(jù)。在視圖的使用中,可以通過控制器給視圖添加動態(tài)數(shù)據(jù),這些數(shù)據(jù)在控制器里都是以數(shù)組鍵值對的形式定義的,在控制器加載視圖的同時,數(shù)據(jù)數(shù)組做為參數(shù)傳遞給視圖;在視圖中,我們只需要知道數(shù)據(jù)在數(shù)組中的鍵名就可以取到想要的數(shù)據(jù)。比如,在控制器里定義的數(shù)據(jù)數(shù)組是:
$data = array('num1' => 1, 'num2' => 2, 'op' => +, 'result' => 3);
那么在視圖中,只需要知道鍵名,就可以取得相對應(yīng)的數(shù)據(jù),比如:
echo $num1." ".$op." ".$num2." = ".$result."<br />";
寫好了用于顯示計算結(jié)果的視圖,也學(xué)會了怎樣給視圖添加動態(tài)數(shù)據(jù),現(xiàn)在只需要稍微修改一下前面寫好的控制器的count函數(shù),計算結(jié)果就可以顯示在result_view視圖上了,對count函數(shù)修改后的代碼如下:
function count() { // 使用輸入類接收參數(shù) $num1 = $this->input->post('num1'); $op = $this->input->post('operate'); $num2 = $this->input->post('num2'); if (is_numeric($num1) && is_numeric($num2)) { // 如果兩個數(shù)輸入均為數(shù)字,則調(diào)用calculate_model模型下的count方法 $result = $this->calculate_model->count($num1, $num2, $op); // 生成要傳給視圖的數(shù)據(jù) $data = array('num1' => $num1, 'num2' => $num2, 'op' => $op, 'result' => $result); // 加載視圖 $this->load->view('result_view', $data); }}可以看到,上面的代碼中,只是將用戶輸入的數(shù)字和操作符以及模型返回的計算結(jié)果放入data數(shù)組中,然后將數(shù)組做為加載視圖時的第二個參數(shù),就可以實現(xiàn)在視圖中顯示計算結(jié)果。看到這里,恭喜你,你已經(jīng)成功的學(xué)會了MVC框架的基本內(nèi)容,其實一點都不難,在接下來的課程中,我將逐步講解CI框架中其他類庫和輔助函數(shù)的使用方法。總結(jié):今天學(xué)習(xí)了CI框架的MVC框架的基本內(nèi)容,到此位置,MVC的基本功能已經(jīng)實現(xiàn),下一講的內(nèi)容是URI以及使用ajax。第二課源代碼下載地址:http://www.youarebug.com/forum.php?mod=viewthread&tid=105&page=1&extra=#pid142
新聞熱點
疑難解答