CodeIgniter开发实际案例-新闻网站【转】

 

CodeIgniter开发实际案例-新闻网站

转:http://blog.csdn.net/ict2014/article/details/22104711?utm_source=tuicool&utm_medium=referral

标签: codeigniter新闻框架示例
 分类:
 

1、建立数据库

      运用Navicat For MySQL工具,创建一个数据库,名称为"news",

      并建立如下表(鼠标右键,命令行运行如下sql语句):

 

[sql] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. CREATE TABLE news (  
  2.     id int(11) NOT NULL AUTO_INCREMENT,  
  3.     title varchar(128) NOT NULL,  
  4.     slug varchar(128) NOT NULL,  
  5.     text text NOT NULL,  
  6.     PRIMARY KEY (id),  
  7.     KEY slug (slug)  
  8. );  

    建立完数据库以及表之后,刷新数据库,然后双击打开news表,填充两条内容。

 

    第一条:(title slug text) 分别为(1,first,Nice Weather!)

    第二条:(title slug text) 分别为(2,second, Pray for MH370!)

2、建立Model模型

      在本系列第二讲中,已经将codeigniter安装包拷贝到了wampserver的www目录下。

      在codeigniter文件夹中,我们在application/models下新建一个文件,名称为“news_model.PHP

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. class News_model extends CI_Model {  
  3.   
  4.     public function __construct()  
  5.     {  
  6.         $this->load->database();  
  7.     }  
  8.       
  9.     public function get_news($slug = FALSE)  
  10.     {  
  11.         if ($slug === FALSE)  
  12.         {  
  13.             $query = $this->db->get('news');  
  14.             return $query->result_array();  
  15.         }  
  16.   
  17.         $query = $this->db->get_where('news', array('slug' => $slug));  
  18.         return $query->row_array();  
  19.     }  
  20.   
  21.     public function set_news()  
  22.     {  
  23.         $this->load->helper('url');  
  24.   
  25.         $slug = url_title($this->input->post('title'), 'dash', TRUE);  
  26.   
  27.         $data = array(  
  28.             'title' => $this->input->post('title'),  
  29.             'slug' => $slug,  
  30.             'text' => $this->input->post('text')  
  31.         );  
  32.   
  33.         return $this->db->insert('news', $data);  
  34.     }  
  35.       
  36. }  
  37. ?>  

     model必须继承CI_Model,构造函数用于加载数据库,get_news用于读取数据库中的新闻,set_news用于插入一条新闻记录。

 

3、建立View

    在application下新建两个文件夹,templates和news。

    在templates文件夹下,新建两个文件,header.php和footer.php。

    header.php的内容如下:

 

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <html>  
  2. <head>  
  3.     <title><?php echo $title ?> - News</title>  
  4. </head>  
  5. <body>  
  6.     <h1>News</h1>  

   footer.php的内容如下:

 

 

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <strong>© 2011</strong>  
  2. </body>  
  3. </html>  

    在news文件夹下,新建四个文件,index.php, success.php, view.php和create.php。

 

    index.php内容如下:

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php foreach ($news as $news_item): ?>  
  2.   
  3.     <h2><?php echo $news_item['title'] ?></h2>  
  4.     <div id="main">  
  5.         <?php echo $news_item['text'] ?>  
  6.     </div>  
  7.     <p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>  
  8.   
  9. <?php endforeach ?>  

   success.php内容如下:

 

 

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. Success  

    view.php内容如下:

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2.     echo '<h2>'.$news_item['title'].'</h2>';  
  3.     echo $news_item['text'];  
  4. ?>  

    create.php内容如下:

 

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <h2>Create a news item</h2>  
  2.   
  3. <?php echo validation_errors(); ?>  
  4.   
  5. <?php echo form_open('news/create') ?>  
  6.   
  7.     <label for="title">Title</label>  
  8.     <input type="input" name="title" /><br />  
  9.   
  10.     <label for="text">Text</label>  
  11.     <textarea name="text"></textarea><br />  
  12.   
  13.     <input type="submit" name="submit" value="Create news item" />  
  14.   
  15. </form>  

4、建立Controller

 

   在application/controllers下新建文件news.php。

   news.php文件内容如下:

 

[php] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. class News extends CI_Controller {  
  3.   
  4.     public function __construct()  
  5.     {  
  6.         parent::__construct();  
  7.         $this->load->model('news_model');  
  8.     }  
  9.   
  10.     public function index()  
  11.     {  
  12.         $data['news'] = $this->news_model->get_news();  
  13.         $data['title'] = 'News archive';  
  14.   
  15.         $this->load->view('templates/header', $data);  
  16.         $this->load->view('news/index', $data);  
  17.         $this->load->view('templates/footer');  
  18.     }  
  19.   
  20.     public function view($slug)  
  21.     {  
  22.         $data['news_item'] = $this->news_model->get_news($slug);  
  23.   
  24.         if (empty($data['news_item']))  
  25.         {  
  26.             show_404();  
  27.         }  
  28.   
  29.         $data['title'] = $data['news_item']['title'];  
  30.   
  31.         $this->load->view('templates/header', $data);  
  32.         $this->load->view('news/view', $data);  
  33.         $this->load->view('templates/footer');  
  34.     }  
  35.   
  36.     public function create()  
  37.     {  
  38.         $this->load->helper('form');  
  39.         $this->load->library('form_validation');  
  40.   
  41.         $data['title'] = 'Create a news item';  
  42.   
  43.         $this->form_validation->set_rules('title', 'Title', 'required');  
  44.         $this->form_validation->set_rules('text', 'text', 'required');  
  45.   
  46.         if ($this->form_validation->run() === FALSE)  
  47.         {  
  48.             $this->load->view('templates/header', $data);  
  49.             $this->load->view('news/create');  
  50.             $this->load->view('templates/footer');  
  51.   
  52.         }  
  53.         else  
  54.         {  
  55.             $this->news_model->set_news();  
  56.             $this->load->view('news/success');  
  57.         }  
  58.     }  
  59.   
  60. }  
  61. ?>  

   Controller用于加载news_model以及生成view视图。其中,除了构造函数之外,其他的每一个函数对应一个界面。

 

5、修改配置文件

   修改数据库文件,在application/config下,打开database.php,修改如下内容,添加数据库、用户名、密码等信息。

   

   修改application/config下的routes.php,输出已有的两行代码,添加如下内容,

 

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. $route['news/create'] = 'news/create';  
  2. $route['news/(:any)'] = 'news/view/$1';  
  3. $route['news'] = 'news';  
  4. $route['(:any)'] = 'pages/view/$1';  
  5. $route['default_controller'] = 'welcome';  

6、测试

 

在浏览器中输入如下网址,

http://127.0.0.1/codeigniter/index.php/news

可以看到如下页面:

 

输入如下网址:

http://127.0.0.1/codeigniter/index.php/news/create

可以看到如下添加新闻的界面:

同时两个页面中都有一些链接,可以点击,对应着views/news下的几个文件。

 

总结:CodeIgniter是基于MVC架构的。只要相应的开发model、view以及controller即可。model用于管理数据,view用于显示,controller充当中介者,用于管理model以及view以及其他资源。学习框架最好的方式,就是搭建一个简单的项目,并且阅读其中的代码。要学习model、view以及controller的代码。

posted @ 2016-05-28 19:05  yuan.net  阅读(316)  评论(0编辑  收藏  举报