codeigniter and form submit

2019-08-02 17:44发布

问题:

I am trying to get used to Codeigniter. I am sorry if that is a trivial or dumb question but I have been struggling to have the "News Section" of Codeigniter's tutorial work.

There is this form (from 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>

which, according to this controller:

<?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');
            }
        }
}

should, I think, if the validation returns ok, go ahead and insert the data into the db. Now, my problem is that the pages run under:

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

The submit button, however, returns me to:

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

The routes.php file contains the following code:

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

I don't know why this is happening. Thank you for any help.

回答1:

Did you set your base_url config in application/config/config.php ?