1
好的,所以我昨天晚上刚开始学习CodeIgniter,并且我即将完成本教程的最后部分,但是,每当我单击提交时,我都会收到一个404页面未找到错误按钮。CodeIgniter教程表单提交出错
这里是它会告诉你创建一个表单和数据存储到数据库教程的最后一部分:http://www.codeigniter.com/user_guide/tutorial/create_news_items.html
我对形式的网址是http://localhost/ci/index.php/news/create
但后来当我点击提交按钮时,URL转向http://localhost/ci/index.php/news/localhost/ci/index.php/news/create
奇怪,我知道那里有与路由选择的问题,但我不知道具体的。
这是我的路线文件:
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
控制器:
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');
}
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');
}
}}
型号:
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);
}}
形式:
'
'标记丢失。核实。 –请重新加载页面,我将片段更改为图片。谢谢 – flix
尝试更改'form_open('news/create');'to'form_open(base_url('news/create'));'。 –