2016-07-27 41 views
0

我最近開始使用codeigniter,我有我的路由幾個問題。儘管閱讀文檔,但我覺得我可能錯過了某些工作的意圖。CodeIgniter新聞站點(路由索引到不同的功能)

我目前設法路由默認控制器執行正確的方法在我有一個類,但是當我嘗試相同的過程說,第二個函數與參數,我得到一個頁面找不到錯誤。我的路由是否在這裏,或者其中一種方法存在問題?

路由:

$route['(:any)'] = 'front/index/$1'; 
$route['/category/(:any)'] = 'front/category/$1'; 
$route['default_controller'] = 'front'; 

數據庫模型(News_model.php)

class News_model extends CI_Model { 

     public function __construct() { 
       $this->load->database(); 
     } 

     public function get_news_latest() { 
      $query = $this->db->get('chanl_posts', 20); 
      return $query->result_array();  
     } 

     public function get_news_category($category) { 
      $query = $this->db->get_where('chanl_posts', array('category' => $category)); 
      return $query->row_array(); 
     } 
} 

控制器(Front.php)

class Front extends CI_Controller { 

     public function __construct() 
     { 
       parent::__construct(); 
       $this->load->model('news_model'); 
       $this->load->helper('url_helper'); 
     } 

     public function category($category) 
     { 
       $data['news_item'] = $this->news_model->get_news_category($category); 

       if (empty($data['news_item'])) 
       { 
         show_404(); 
       } 

       $this->load->view('templates/header', $data); 
       $this->load->view('front/index', $data); 
       $this->load->view('templates/footer'); 
     } 

     public function index() { 
       $data['news'] = $this->news_model->get_news_latest(); 

       $this->load->view('templates/header', $data); 
       $this->load->view('front/index', $data); 
       $this->load->view('templates/footer'); 
     } 
} 
+0

爲什麼你要爲索引傳遞一個參數? $ route ['(:any)'] ='前面/索引/ $ 1'; 順便說一句,你的索引函數不期待任何參數 index() –

回答

0

嘗試這些路線

$route['category'] = 'front'; 
$route['category/(:any)'] = 'front/category/$1'; 
$route['default_controller'] = 'front';