Models部分负责读取或写入数据库
<?php class News_model extends CI_Model { public function __construct() { //加载数据库,后面就可以用$this->db $this->load->database(); } public function get_new_by_id($id) { $query = $this->db->get_where('news', array('id' => $id)); //获取指定数目的新闻 return $query->row_array(); } 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() { //加载url辅助函数 $this->load->helper('url'); //使用辅助函数,使用"abc def"的title转变成abc-def,在数据库中可以看到 //获取表单提交的数据,可以看到slug就是对标题处理后的结果 $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); } }
Views部分的PHP脚本负责动态生成html页面
views/news/index.php,生成所有新闻
1 <!--从$news中取出新闻条目,存放在$news_item中 --> 2 <?php foreach ($news as $news_item): ?> 3 <!-- 取出标题--> 4 <h3><?php echo $news_item['title']; ?></h3> 5 <!-- 取出文本 --> 6 <div class="main"> 7 <?php echo $news_item['text']; ?> 8 </div> 9 <!-- 将产生http://localhost/CodeIgniter/index.php/news/www-good 10 经由路由规则,将会调用News控制器的view方法,并把www-good作为参数传递进去,参看News.php的view方法 11 会生成一个新网页 12 --> 13 <p><a href="<?php echo site_url('news/'.$news_item['id']); ?>">View article</a></p> 14 15 <?php endforeach; ?>
views/news/create.php
1 <h2><?php echo $title; ?></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>
views/news/view.php,生成新闻内容
1 <?php 2 echo $news_item['text'];
views/templates/header.php,负责生成页面开头某一部分
1 <html> 2 <head> 3 <title>CodeIgniter Tutorial</title> 4 </head> 5 <body> 6 7 <h1><?php echo $title; ?></h1>
views/templates/footer.php,负责生成页页结尾某一部分
1 </body> 2 </html>
News控制器负责根据URL参数来调用Models和Views,最终完成数据库存取和页面生成
如:http://localhost/codeigniter/news/views/6
这个URL访问,将调用news控制器的viewx方法,并传入参数6
1 <?php 2 class News extends CI_Controller { 3 4 public function __construct() 5 { 6 //调用父类的构造函数 7 parent::__construct(); 8 //加载模型, 下面的create函数会用到 9 $this->load->model('news_model'); 10 //加载URL 11 $this->load->helper('url_helper'); 12 } 13 14 public function index() 15 { 16 //从模型中获取所有的新闻条目, $data数组在模型中创建 17 $data['news'] = $this->news_model->get_news(); 18 //页面标题 19 $data['title'] = 'News archive'; 20 //加载页头, header.php会访问$data中的$title元素 21 $this->load->view('templates/header', $data); 22 //加载主视图,index.php会访问$data数组中的$news元素,并显示$news中的所有新闻 23 $this->load->view('news/index', $data); 24 //加载页脚 25 $this->load->view('templates/footer'); 26 27 } 28 //id, title, slug, text 29 public function view($slug = NULL) 30 { 31 //获取指定条目的新闻 32 /* 33 $data['news_item'] = $this->news_model->get_news($slug); 34 35 if (empty($data['news_item'])) 36 { 37 show_404(); 38 } 39 40 $data['title'] = $data['news_item']['title']; 41 42 $this->load->view('templates/header', $data); 43 $this->load->view('news/view', $data); 44 $this->load->view('templates/footer'); 45 */ 46 $data['news_item'] = $this->news_model->get_new_by_id($slug); 47 48 if (empty($data['news_item'])) 49 { 50 show_404(); 51 } 52 53 $data['title'] = $data['news_item']['title']; 54 55 $this->load->view('templates/header', $data); 56 $this->load->view('news/view', $data); 57 $this->load->view('templates/footer'); 58 } 59 60 public function create() 61 { 62 $this->load->helper('form'); 63 $this->load->library('form_validation'); 64 65 $data['title'] = 'Create a news item'; 66 67 $this->form_validation->set_rules('title', 'Title', 'required'); 68 $this->form_validation->set_rules('text', 'Text', 'required'); 69 70 if ($this->form_validation->run() === FALSE) 71 { 72 //页头会从$data中取出$title数据 73 $this->load->view('templates/header', $data); 74 $this->load->view('news/create'); 75 $this->load->view('templates/footer'); 76 77 } 78 else 79 { 80 $this->news_model->set_news(); 81 $this->load->view('news/success'); 82 } 83 } 84 }