路由:/application/config/routes.phpphp
$route['news/create'] = 'news/create'; $route['news/(:any)'] = 'news/view/$1'; $route['news'] = 'news'; $route['(:any)'] = 'pages/view/$1'; $route['default_controller'] = 'pages/view';
控制器:/application/controllers/News.phphtml
<?php /** * 新聞控制器 */ class News extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('news_model'); $this->load->helper('url_helper'); } // 呈現全部數據-列表頁 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', $data); } // 呈現某條新聞-詳情頁, 若是查出多條新聞只取第一條 public function view($slug = NULL) { $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', $data); } // 表單提交數據-建立 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'); } } }
模型:/application/models/News_model.phpapp
<?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); } }
建立表:post
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) );
視圖:/application/views/ui
# news/create.php <h2><?php echo $title ?></h2> <?php echo validation_errors(); ?> <?php echo form_open('news/create'); ?> <label for="title">Title</label> <input type="text" name="title"><br /> <label for="text">Text</label> <textarea name="text" ></textarea><br /> <input type="submit" name="submit" value="Create news item"> </form> # news/index.php <h2><?php echo $title; ?></h2> <?php foreach ($news as $news_item): ?> <h3><?php echo $news_item['title'] ?></h3> <div class="main"> <?php echo $news_item['text'] ?> </div> <p><a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a></p> <?php endforeach; ?> # news/success.php <?php echo 'success'; # news/view.php <?php echo '<h2>'.$news_item['title'].'</h2>'; echo $news_item['text'];
# templates/header.php