【发布时间】:2016-07-23 11:49:48
【问题描述】:
好的,所以我昨晚刚开始学习 CodeIgniter,今天我即将完成教程的最后一部分,但是,每当我单击提交按钮时,我都会收到 404 page not found 错误。
这是教程的最后一部分,它告诉您创建一个表单并将数据存储到数据库中: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);
}}
表格:
【问题讨论】:
-
<form action=""></form>标签丢失。检查一下。 -
请重新加载页面,我将 sn-p 更改为图像.. 谢谢
-
尝试将
form_open('news/create');更改为form_open(base_url('news/create'));。 -
还是不行:(
-
您是否在配置文件中设置了基本网址?
标签: php codeigniter model-view-controller