【发布时间】:2017-01-09 12:59:10
【问题描述】:
我正在尝试习惯 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');
}
}
}
我认为,如果验证返回正常,请继续将数据插入数据库。现在,我的问题是页面运行在:
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';
我不知道为什么会这样。感谢您的帮助。
【问题讨论】:
-
$config['base_url'] = 'http://localhost/codeigniter/;
标签: php forms codeigniter