表单提交时CodeIgniter教程错误

时间:2016-04-04 03:31:55

标签: php codeigniter model-view-controller

好的,所以我刚刚开始学习CodeIgniter昨晚我即将完成教程的最后一部分,但是,每当我点击提交按钮时,我都会收到404页面未找到错误。

以下是本教程的最后一部分,它告诉您创建表单并将数据存储到数据库:http://www.codeigniter.com/user_guide/tutorial/create_news_items.html

表单的网址为http://localhost/ci/index.php/news/create

但是当我点击提交按钮时,网址会转为http://localhost/ci/index.php/news/localhost/ci/index.php/news/create

enter image description here

很奇怪,我知道路由有问题,但我不知道具体的那些。

这是我的路线档案:

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

形式:

enter image description here

1 个答案:

答案 0 :(得分:0)

解决方案:

  1. 打开application \ config \ config.php
  2. 寻找$ config [' base_url']
  3. 将其设置为$ config [' base_url'] ='';