?
codeigniter 的程序流程
1. ?????? 設計視圖
首先,讓我們設計視圖并把它保存到如下路徑: system/application/views/testview.php
- <html>??
- <head>??
- ??< !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" >??
- ??<title>Web?test?Site</title>???
- </head>??
- <body>??
- ??<h1><?php? echo ? $mytitle ;??>?</h1>??
- ??<p? class = "test" >?<?php? echo ? $mytext ;??>?</p>??
- </body>??
- </html>??
???????
? ? ? ? 你還可以聯想到,變量 $mytitle 和 $mytext 的內容呢?答案是我們需要一個新的控制器!
?
2. ??????? 設計控制器
現在,我們需要一個新的控制器。我們將其命名為 Start 并保存在: /system/application/controllers/start.php
該控制器必須做幾件事:
·????????? 調用視圖
·????????? 把一些數據傳遞給視圖:它正在期待標題( $mytitle )和一些本文( $mytext )
·????????? 最后,我們想要控制器接受來自使用者的一個參數(例如通過 URL 請求)
- <?php
- class ?Start? extends ?CI_Controller?{
- function ?__construct()
- {
- ????? parent::__construct();
- }
代碼片段2 控制器構造函數
? ? ? ? 在上面的代碼中可以看出 Start 是一個構造函數,他完成了控制器的初始化。
?????? 下面是將參數傳遞個一個函數的代碼:
- function ?hello( $name )??
- {??
- ???? $data [ 'mytitle' ]?=? 'Welcome?to?this?site' ;??
- ???? $data [ 'mytext' ]?=? "Hello,?$name,?now?we're?getting?dynamic!" ;??
- ???? $this ->load->view( 'testview' ,? $data );??
- }??
- ?>??
???? 這個函數期待一個參數, $name ,可能會產生疑問 $name 變量從哪來?其實它需要來自 URL 請求的第三個參數,話句話說換句話說,當你輸入 URL :
http://www.mysite.com/index.php/start/hello/fred
URL 的最后一段作為一個參數傳給函數 hello ,倒數第二個參數是指當前的函數名是 hello ,倒數第三個函數是指當前的控制器名字是 start 。當然我們還會看到 index.php 這個參數,這是由于 CI 將所有的請求都發送到了 index.php 文件中,你也可以用 .htaccess 文件重寫 URL 來去掉 index.php ,考慮到服務器可能不支持 .htaccess ,我們沒有做出修改。
???? 再回到對控制器的討論中,注意 hello() 函數如何先設置一個名為 $data 的數組,并把一些對象的屬性及文本讀入數組。然后它通過名稱裝載視圖,并把剛生成的 $data 數組作為第二個參數。在幕后, CI 很好地利用了另外一個 PHP 函數: extract() ,這個函數的作用是把數組中的元素取出放入變量表,其中每個鍵值對中的鍵名即為變量名,對應該鍵名的值為變量的值。因此我們剛才定義的 $data 數組在視圖中轉換成一個單一的變量: $text (等于 “Hello, $name, now we're getting dynamic” )。
???? 換句話說,當它被建立的時候, $data 數組看起來像這樣:
???? Array
(?
??[mytitle]?=>?'Welcome to this site',
??[mytext]?=>?"Hello, fred, now we're getting dynamic!"
) ;
???? 但是當它被傳遞給視圖的過程中,它被解開,并且下列變量在視圖對象中生成,與 $data 的每個鍵 / 值相對應: ?????
???? $mytitle? ??=???'Welcome to this site';
?????$mytext????=???"Hello, fred, now we're getting dynamic!";
???? 雖然你只傳送一個變量到視圖中,但是,你能把許多數據裝進那個變量中。 $data 數組的每個值還可以是數組,這被稱為多維數組,因此,用一個數組變量可以把大量的變量傳遞給視圖。
?
???? 3. 設計模型
??????? 其實模型的設計要在控制器之前完成的,但是為了演示控制器和視圖之間的數據流動,我們把控制器和視圖放到一起說了。
??????? 所以說上面介紹的只是 VC ,因為還沒介紹到 M ,也就是模型。下面構造一個MVC的完整流程。
??????? 在第二部分介紹控制器時給數組 $data 的元素賦值時,我們用到的都是常量,其實在實際的編碼情況中,我們的數據都是從數據庫中讀出的,這份工作就是下面要說到的控制器的職責。先看下面一部分代碼: ? ? ?
- <?php??
- ????? class ?Art? extends ?CI_Model?{??
- ???????????? public ? function ?__construct()?{??
- ???????????????????parent::__construct();??
- ??????????????????? //$this->load->library('database');//在autoload.php中配置了自動加載database類 ??
- ????????????}??
- ???????????? public ? function ?getList( $name )?{??
- ??????????????????? $this ->db->select( 'title,content' );?
- ? ?if (!is_null( $name )) {?
- ??????????????????? $this ->db->where( 'author' ,? $name );??
- ? ?}
- ??????????????????? $result = $this ->db->get( 'art' );??
- ??????????????????? return ? $result ->num_rows()>0??? $result ->result_array()?:?null;??
- ????????????}??
- ??
- ??
- ?????}??
- ?>??
?
?????? 首先我們看到這也是一個類,繼承自父類 Model ,首先由 __construct 函數完成該類的初始化。并加載了 CI 的數據庫類 ( 也就是我們前面所有的 AR) 。然后我們看類中的 getOne 函數,這是要介紹的重點。
??????$this->db->select(‘title,content’);
??????$this->db->where(‘ author ’,$name);
??????$result=$this->db->get(‘art’);
這三句話放到普通的 php 中應該是這么寫的:
$sql =”select title,content from art?where author =’“.$name.”’”;
$result =mysql_query($sql);??
?
可以看出用了 AR 后數據庫的處理變得簡潔明了,并且更重要的是 CI 的 AR 生成隱含的代碼,在幕后進行轉意和類型轉化,使代碼更加健壯。
?????? 我們在模型中將數據進行處理(查詢、修改、刪除等),然后將處理結果交給控制器處理。控制器在這里起到了路由的作用,它會將處理的結果交給不同的視圖去處理。
在控制器中我們這么取出數據:
??????
- <?php??
- class ?Artmanager? extends ?CI_Controller?{??
- ???? function ?__construct()?{??
- ????????parent::__construct();??
- ???????? $this ->load->model( 'art' );??
- ????}??
- ??
- ???? function ?index( $authName )?{?
- //$this->load->library('unit_test'); ?
- ???????? $list ?=? $this ->art->getList( $authName );??
- ???????? $data [ 'list' ]?=? $list ;??
- ???????? $this ->load->view( 'art_list' , $data );?
- //$this->output->enable_profiler(TRUE);?
- ????}??
- }??
- $this ->load->model( 'art' );???????????????
- $list ?=? $this ->art->getList( $authName ); ?
- 最后一句使用來加載視圖的: ? ? ?
- ? ? $this ->load->view( 'art_list' , $data ); ? ??
- 使用這一句將 $data 數組中的數據傳遞到art_list.php(文件存放位置稍后給出)這個文件中。現在看一下art_list.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>文章列表</title>??
- ? ??<base href="<?php echo base_url();?>" />
- ????<meta?name= "keywords" ?content= "" ?/>??
- ????<meta?name= "description" ?content= "" ?/>??
- ? ??<link href="css/index.css" rel="stylesheet"?type="text/css" />
- </head>??
- ??
- <body>??
- ????<table? class = "list" >??
- ????????<tr>??
- ????????????<th></th><th>標題</th><th>內容</th>??
- ????????</tr>??
- ??
- ????<?php??
- ???????? if ?( is_null ( $list ))?{??
- ?????>??
- ????????<tr><td?colspan= "3" >沒有數據</td></tr>??
- ????<?php??
- ????????}? else ?{??
- ???????????? foreach ?( $list ? as ? $index => $article )?{??
- ?????>??
- ????????????????<tr>??
- ????????????????????<td><?php? echo ? $index +1;?></td>??
- ????????????????????<td><?php? echo ? $article [ 'title' ]?></td>??
- ????????????????????<td><?php? echo ? $article [ 'content' ]?></td>??
- ????????????????</tr>??
- ????<?php??
- ????????????}??
- ????????}??
- ?????>??
- ??
- ????</table>??
- </body>??
- </html>??
代碼片段6 視圖中解析控制器中的參數
? ? 注意代碼片段6中的: ?
- foreach ?( $list ? as ? $index => $article )??
為了讓上述代碼能夠正確的運行起來,還需要做一些響應的配置。首先,在application/config/database.php中配置數據庫連接:
?
$db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'mycms';
?然后在application/config/autoload.php中定義自動加載的類庫,修改一下里面的相應代碼就可以了:
?
/* | ------------------------------------------------------------------- | Auto-load Libraries | ------------------------------------------------------------------- | These are the classes located in the system/libraries folder | or in your application/libraries folder. | | Prototype: | | $autoload['libraries'] = array('database', 'session', 'xmlrpc'); */ $autoload['libraries'] = array('database', 'session'); /* | ------------------------------------------------------------------- | Auto-load Helper Files | ------------------------------------------------------------------- | Prototype: | | $autoload['helper'] = array('url', 'file'); */ $autoload['helper'] = array('url');?
? 雖然這篇教程中沒有用到session,但是按照我的慣例,我還是把他配置上了。如果你在運行的時候,出現了如下提示:
?
An Error Was Encountered
In order to use the Session class you are required to set an encryption key in your config file.
那么修改一下 application/config/autoload.php中的 encryption_key元素即可,隨便輸入幾個字符:
?
/* |-------------------------------------------------------------------------- | Encryption Key |-------------------------------------------------------------------------- | | If you use the Encryption class or the Session class you | MUST set an encryption key. See the user guide for info. | */ $config['encryption_key'] = 'FSJFEO@¥#%)#(¥02334';?
注:以上內容改編自《codeigniter敏捷開發框架》中的部分內容。詳細代碼以及數據庫文件,見附件。
本教程基于ci2.x編寫,基于1.x的教程可參見我的csdn博客 http://blog.csdn.net/yunnysunny/article/details/6214171
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
