我今天刚刚开始使用CodeIgniter,并在the CI website处遵循了教程。
当我到达创建新新闻的最后一步时,我遇到了表单提交的问题。当我按下表单的提交按钮时,我希望URL为jonhopkins.net/news/create
,但它会转到http://jonhopkins.net/news/jonhopkins.net/news/create
,这显然是非常错误的,我不知道为什么会发生这种情况。有谁知道如何解决这一问题?
我网站上的其他所有内容(教程的Pages部分)都运行良好。这是唯一破坏的东西。我的所有代码都与教程中显示的完全一样,除了对项目成功提交到数据库时发生的事情的更改,当前这种情况从未发生过。
新闻控制器
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 {
//$success will contain the slug of the created item, or FALSE if database insertion failed
$success = $this->news_model->set_news();
if ($success) {
$this->_doView($success);
} else {
echo "Something went wrong while trying to submit the news item.";
}
}
}
public function view($slug) {
$this->_doView($slug);
}
private function _doView($slug) {
$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');
}
创建视图
<h2>Create a news item</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>
结果HTML ......
<form action="jonhopkins.net/news/create" method="post" accept-charset="utf-8">
<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>
新闻模式我不认为它会在这里,但以防万一
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'),
'date' => date("Y-m-d H:i:s")
);
if ($this->db->insert('news', $data)) {
return $slug;
} else {
return FALSE;
}
}
路线
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
答案 0 :(得分:5)
form_open('news/create')
方法使用$CI->Config->site_url()
方法,而后者则使用配置文件中的base_url
。
确保$config['base_url']
文件中的application/config/config.php
设置为您的域(带斜杠)。
$config['base_url'] = 'http://jonhopkins.net/';