2016-09-01 90 views
1

我想要習慣Codeigniter。我很抱歉,如果這是一個微不足道或愚蠢的問題,但我一直在努力讓Codeigniter的教程的「新聞部分」工作。codeigniter和表單提交

有這種形式(從here

<h2><?php echo $title; ?></h2> 

<?php echo validation_errors(); ?> 

<?php echo form_open('news/create'); ?> 

    <label for="title">Title</label> 
    <input type="input" name="title" /><br /> 

    <label for="text">Text</label> 
    <textarea name="text"></textarea><br /> 

    <input type="submit" name="submit" value="Create news item" /> 

</form> 

其中,根據this控制器:

<?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'] = 'My News archive'; 

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

     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/', $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'); 
      } 
     } 
} 

應該,我認爲,如果驗證返回OK,繼續前進,插入數據進入數據庫。現在,我的問題是,網頁下運行:

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

提交按鈕,然而,返回我:

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

的routes.php文件文件包含以下代碼:

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

我不知道爲什麼會發生這種情況。感謝您的任何幫助。

+1

'$ config ['base_url'] ='http:// localhost/codeigniter /;' – Sparky

回答

1

您是否在application/config/config.php中設置了base_url配置?

+0

非常感謝您的回覆。在我的config.php中有這樣一行:$ config ['base_url'] ='localhost/codeigniter';應該設置base_url。 – moijoune

+0

你的意思是這個$ config ['base_url'] ='http:// localhost/codeigniter /'; –

+0

難道是這個嗎?哇!!謝謝!我正在嘗試您的建議! – moijoune