php - 为什么我的 URL 重定向不起作用?

标签 php codeigniter codeigniter-2 codeigniter-url

我下载了 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>&copy;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/

相关文章:

php - 正则表达式删除特定括号内的内容

php - 在不读取文件的情况下检查文件是否存在于不同的域中

php - CodeIgniter 中这些文件的用途是什么?

php - IIS 上带有 web.config 文件的 Codeigniter 2

php - 从 2 个带有 2 个外键的表中选择

CodeIgniter:分页没有问号?

php - 无法登录 Php MySQL android

php - 警告: mysqli::mysqli(): (HY000/1045): 用户 'database' @'localhost' 的访问被拒绝(使用密码:NO)

php - 查询以从相关表中获取数据

regex - Drupal URL重写冲突