【问题标题】:Why does my URL redirect not work?为什么我的 URL 重定向不起作用?
【发布时间】:2012-01-15 19:51:58
【问题描述】:

我下载了 CodeIgniter 2.1.0,并在 CodeIgniter 2.1.0 上关注了tutorial

问题是当我尝试在此 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函数即可。 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 Pages Not Found

【问题讨论】:

  • 您的表单的action 是什么?我知道你在使用form_open(),但请幽默并检查 HTML 输出。
  • 表格是向数据库中插入一条新的新闻,包括新闻标题和新闻正文。
  • 所有表单都有一个action 属性,我在问你的HTML输出中那个值是什么。
  • OK,表单页面将由上面的create()函数生成。如果表单验证器运行没有错误,它会将页面重定向到success.php,它只会告诉你已经提交了一个表单;否则它将重新加载 for 页面。
  • 请在您查看表单时将浏览器中的 HTML 提供给我们

标签: php codeigniter codeigniter-2 codeigniter-url


【解决方案1】:

我找到了解决办法,是因为base url设置不当造成的。

在我的基本网址之前是:$config['base_url'] = 'localhost/CodeIgniter';

现在我改成

$config['base_url'] = 'http://localhost/CodeIgniter/';

它现在工作正常。

感谢任何试图提供帮助的人:)

【讨论】:

    【解决方案2】:

    只是一个建议,可能对你有帮助:

    $config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
    $config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
    

    在你的 config.php 文件中设置它,你永远不会担心$config['base_url']。因为它将设置httphttps 端口、domainport number(例如8080、8880 等)和codeigniter root folder。此外,它还可以在您的任何 codeigniter 项目中重复使用。 :)

    您也可以查看this article

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2015-03-19
      • 1970-01-01
      相关资源
      最近更新 更多