2012-01-02 166 views
3

繼隨着CI News TutorialCodeIgniter的路由,404錯誤

我只是做了新聞欄目,所以我改變了默認控制器「新聞」

$route['news/create'] = 'news/create'; 
$route['news/(:any)'] = 'news/view/$1'; 
$route['news'] = 'news'; 
$route['default_controller'] = 'news'; 

現在從生成一個404錯誤'查看文章'錨點。將默認值更改爲:

$route['default_controller'] = 'welcome'; 

創建正確的路徑。我應該如何改變路由器使用新聞?

沒有自定義配置或使用.htaccess。

從config.php文件:

$config['base_url'] = 'http://frameworks:8888/ci_site_tut/'; 

$config['index_page'] = 'index.php'; 

新聞控制器:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
/** 
* 
*/ 
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'); 

     } 



    } 

} 

SOLUTION:

自動加載URL助手:

$autoload['helper'] = array('url'); 

級更新的路由:

$route['default_controller'] = 'news'; 
$route['404_override'] = 'errors/page_missing'; 
$route['news/create'] = 'news/create'; 
$route['news/(:any)'] = 'news/view/$1'; 
$route['news'] = 'news'; 

的意見/新聞/ index.php文件更新網址:

<p><a href="<?php echo site_url('news/' . $news_item['slug']); ?>">View Article</a></p> 
+0

你想在瀏覽器中使用什麼網址? – Catfish 2012-01-03 17:04:47

+0

View article

。 index.php不包含在URL中。 – Wasabi 2012-01-04 17:39:54

+0

我有同樣的問題....我認爲這是一個錯字。我把' 2013-10-15 23:50:34

回答

1

它看起來不像你在你的視圖方法中的任何地方設置$ new_item ['slug']。在加載視圖之前查看您的視圖方法中是否存在'print_r($data['news_item'])以查看是否實際設置了slug。

如果您確定自己是,請確保url助手是自動加載的,並嘗試在您的視圖中使用它作爲您的網址。

<a href="<?php echo site_url('news/'.$news_item['slug']); ?>">View article</a> 
+0

您的建議有效,並且也由CI站點推薦。也更新了我的路線。 – Wasabi 2012-01-05 01:27:18

0

你不需要任何的定製路線,只是一個視圖控制器。將索引頁設置爲空白並刪除基本URL。你應該只需要在你的新聞控制器中創建和查看方法。

+0

我有一個視圖控制器,每個教程,自定義路由不需要,由於只有一個控制器(新聞)?謝謝回覆! – Wasabi 2012-01-02 01:05:33

+0

建議仍然會返回404。 – Wasabi 2012-01-02 01:12:27

0

我也遇到了404 page not found路由錯誤。所以我認爲我的回答會幫助遇到同樣問題的一些用戶。這是我的兩分錢。

如果一個人完全按照​​,很可能會遇到上述問題。因爲人按照本教程最有可能來自於前面的教程頁面並追加新的路由配置到

$route['news/(:any)'] = 'news/view/$1'; 
$route['news'] = 'news'; 
$route['(:any)'] = 'pages/view/$1'; 
$route['default_controller'] = "pages/view"; 
$route['404_override'] = ''; 

:下面爲避免你應該做的是什麼錯誤都在順序route configuration正是給出已經存在的讓他們陷入困境的人。

而另一件事是,在News控制器的view方法,改線

$data['news'] = $this->news_model->get_news($slug); 

$data['news_item'] = $this->news_model->get_news($slug); 

他們應該強調的路由配置的順序和刪除錯字在代碼中。希望這可以幫助。