我下载了 CodeIgniter 2.1.0 并遵循了 tutorial在 CodeIgniter 2.1.0 上。
问题是当我尝试在此 URL 中提交表单时:
http://localhost/CodeIgniter/index.php/news/create
页面被重定向到这个 URL:
http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create
我尝试了很多方法来更改 route.php,但它不起作用。我该如何解决这个问题??
route.php
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['(:any)'] = 'pages/view/$1';
$route['default_controller'] = 'pages/view';
这是表单页面的代码:
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');
}
}
我注意到当加载表单页面时,下面的代码段将执行,但是,else 段永远不会执行更改。
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
这是表单页面,没什么特别的,调用上面所示的create函数即可。 创建.php
<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:
<html>
<head>
<title>Create a news item - CodeIgniter 2 Tutorial</title>
</head>
<body>
<h1>CodeIgniter 2 tutorial</h1><h2>Create a news item</h2>
<form action="localhost/CodeIgniter/index.php/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><strong>©2012</strong>
</body>
</html>
当我按下 create new item
按钮时,URL 更改为
http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create
并且页面显示 404 找不到页面
最佳答案
我找到了解决方案,它是由不正确的基础 url 设置引起的。
在我的基本 url 之前是:$config['base_url'] = 'localhost/CodeIgniter';
现在我改成了
$config['base_url'] = 'http://localhost/CodeIgniter/';
现在可以正常使用了。
感谢所有试图提供帮助的人:)
关于php - 为什么我的 URL 重定向不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8872805/