生活随笔
收集整理的這篇文章主要介紹了
CodeIgniter开发实际案例-新闻网站【转】
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
CodeIgniter開發(fā)實際案例-新聞網(wǎng)站
轉(zhuǎn):http://blog.csdn.net/ict2014/article/details/22104711?utm_source=tuicool&utm_medium=referral
標(biāo)簽:?codeigniter新聞框架示例 2014-03-25 22:53?1015人閱讀? ?分類: ? Code Igniter(3)?
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
1、建立數(shù)據(jù)庫
? ? ? 運(yùn)用Navicat For?MySQL工具,創(chuàng)建一個數(shù)據(jù)庫,名稱為"news",
? ? ? 并建立如下表(鼠標(biāo)右鍵,命令行運(yùn)行如下sql語句):
?
[sql]?view plaincopy print?
CREATE?TABLE?news?(??????id?int(11)?NOT?NULL?AUTO_INCREMENT,??????title?varchar(128)?NOT?NULL,??????slug?varchar(128)?NOT?NULL,??????text?text?NOT?NULL,??????PRIMARY?KEY?(id),??????KEY?slug?(slug)??);?? ? ? 建立完數(shù)據(jù)庫以及表之后,刷新數(shù)據(jù)庫,然后雙擊打開news表,填充兩條內(nèi)容。
?
? ? 第一條:(title slug text) 分別為(1,first,Nice Weather!)
? ? 第二條:(title slug text) 分別為(2,second,?Pray for MH370!)
2、建立Model模型
? ? ? 在本系列第二講中,已經(jīng)將codeigniter安裝包拷貝到了wampserver的www目錄下。
? ? ? 在codeigniter文件夾中,我們在application/models下新建一個文件,名稱為“news_model.PHP”
?
[php]?view plaincopy print?
<?php??class?News_model?extends?CI_Model?{????????public?function?__construct()??????{??????????$this->load->database();??????}????????????public?function?get_news($slug?=?FALSE)??????{??????????if?($slug?===?FALSE)??????????{??????????????$query?=?$this->db->get('news');??????????????return?$query->result_array();??????????}????????????$query?=?$this->db->get_where('news',?array('slug'?=>?$slug));??????????return?$query->row_array();??????}????????public?function?set_news()??????{??????????$this->load->helper('url');????????????$slug?=?url_title($this->input->post('title'),?'dash',?TRUE);????????????$data?=?array(??????????????'title'?=>?$this->input->post('title'),??????????????'slug'?=>?$slug,??????????????'text'?=>?$this->input->post('text')??????????);????????????return?$this->db->insert('news',?$data);??????}????????}???>?? ? ? ?model必須繼承CI_Model,構(gòu)造函數(shù)用于加載數(shù)據(jù)庫,get_news用于讀取數(shù)據(jù)庫中的新聞,set_news用于插入一條新聞記錄。
?
3、建立View
? ? 在application下新建兩個文件夾,templates和news。
? ? 在templates文件夾下,新建兩個文件,header.php和footer.php。
? ? header.php的內(nèi)容如下:
?
[html]?view plaincopy print?
<html>??<head>??????<title><?php?echo?$title??>?-?News</title>??</head>??<body>??????<h1>News</h1>?? ? ?footer.php的內(nèi)容如下:
?
?
[html]?view plaincopy print?
<strong>??2011</strong>??</body>??</html>?? ? ? 在news文件夾下,新建四個文件,index.php, success.php, view.php和create.php。
?
? ? index.php內(nèi)容如下:
?
[php]?view plaincopy print?
<?php?foreach?($news?as?$news_item):??>????????<h2><?php?echo?$news_item['title']??></h2>??????<div?id="main">??????????<?php?echo?$news_item['text']??>??????</div>??????<p><a?href="news/<?php?echo?$news_item['slug']??>">View?article</a></p>????<?php?endforeach??>?? ? ?success.php內(nèi)容如下:
?
?
[html]?view plaincopy print?
Success?? ? ? view.php內(nèi)容如下:
?
?
[php]?view plaincopy print?
<?php??????echo?'<h2>'.$news_item['title'].'</h2>';??????echo?$news_item['text'];???>?? ? ? create.php內(nèi)容如下:
?
?
[php]?view plaincopy print?
<h2>Create?a?news?item</h2>????<?php?echo?validation_errors();??>????<?php?echo?form_open('news/create')??>????????<label?for="title">Title</label>??????<input?type="input"?name="title"?/><br?/>????????<label?for="text">Text</label>??????<textarea?name="text"></textarea><br?/>????????<input?type="submit"?name="submit"?value="Create?news?item"?/>????</form>?? 4、建立Controller ?
? ?在application/controllers下新建文件news.php。
? ?news.php文件內(nèi)容如下:
?
[php]?view plaincopy print?
<?php??class?News?extends?CI_Controller?{????????public?function?__construct()??????{??????????parent::__construct();??????????$this->load->model('news_model');??????}????????public?function?index()??????{??????????$data['news']?=?$this->news_model->get_news();??????????$data['title']?=?'News?archive';????????????$this->load->view('templates/header',?$data);??????????$this->load->view('news/index',?$data);??????????$this->load->view('templates/footer');??????}????????public?function?view($slug)??????{??????????$data['news_item']?=?$this->news_model->get_news($slug);????????????if?(empty($data['news_item']))??????????{??????????????show_404();??????????}????????????$data['title']?=?$data['news_item']['title'];????????????$this->load->view('templates/header',?$data);??????????$this->load->view('news/view',?$data);??????????$this->load->view('templates/footer');??????}????????public?function?create()??????{??????????$this->load->helper('form');??????????$this->load->library('form_validation');????????????$data['title']?=?'Create?a?news?item';????????????$this->form_validation->set_rules('title',?'Title',?'required');??????????$this->form_validation->set_rules('text',?'text',?'required');????????????if?($this->form_validation->run()?===?FALSE)??????????{??????????????$this->load->view('templates/header',?$data);??????????????$this->load->view('news/create');??????????????$this->load->view('templates/footer');????????????}??????????else??????????{??????????????$this->news_model->set_news();??????????????$this->load->view('news/success');??????????}??????}????}???>?? ? ?Controller用于加載news_model以及生成view視圖。其中,除了構(gòu)造函數(shù)之外,其他的每一個函數(shù)對應(yīng)一個界面。
?
5、修改配置文件
? ?修改數(shù)據(jù)庫文件,在application/config下,打開database.php,修改如下內(nèi)容,添加數(shù)據(jù)庫、用戶名、密碼等信息。
? ?
? ?修改application/config下的routes.php,輸出已有的兩行代碼,添加如下內(nèi)容,
?
[html]?view plaincopy print?
$route['news/create']?=?'news/create';??$route['news/(:any)']?=?'news/view/$1';??$route['news']?=?'news';??$route['(:any)']?=?'pages/view/$1';??$route['default_controller']?=?'welcome';?? 6、測試 ?
在瀏覽器中輸入如下網(wǎng)址,
http://127.0.0.1/codeigniter/index.php/news
可以看到如下頁面:
?
輸入如下網(wǎng)址:
http://127.0.0.1/codeigniter/index.php/news/create
可以看到如下添加新聞的界面:
同時兩個頁面中都有一些鏈接,可以點擊,對應(yīng)著views/news下的幾個文件。
?
總結(jié):CodeIgniter是基于MVC架構(gòu)的。只要相應(yīng)的開發(fā)model、view以及controller即可。model用于管理數(shù)據(jù),view用于顯示,controller充當(dāng)中介者,用于管理model以及view以及其他資源。學(xué)習(xí)框架最好的方式,就是搭建一個簡單的項目,并且閱讀其中的代碼。要學(xué)習(xí)model、view以及controller的代碼。
轉(zhuǎn)載于:https://www.cnblogs.com/fangyuan303687320/p/5538127.html
總結(jié)
以上是生活随笔為你收集整理的CodeIgniter开发实际案例-新闻网站【转】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。