2012-01-15 67 views
2

我下载了CodeIgniter 2.1.0,我在CodeIgniter 2.1.0上跟着tutorial为什么我的网址重定向不起作用?

问题是,当我试图在这个URL提交表单:

http://localhost/CodeIgniter/index.php/news/create 

页面被重定向到这个网址:

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'); 
    } 
} 

我注意到在加载网页时当表单页面,下面的代码段将执行但是,其他部分从未得到要执行的更改。

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,网址改为

http://localhost/CodeIgniter/index.php/news/localhost/CodeIgniter/index.php/news/create 

和页面显示404 Pages Not Found

+0

什么是您的表单的“行动”?我知道你使用'form_open()',但只是幽默我,并检查HTML输出。 – 2012-01-15 19:54:49

+0

表单是为数据库插入新消息,其中包括新闻标题和新闻正文。 – qkzhu 2012-01-15 20:04:43

+0

所有表单都有一个“action”属性,我在问HTML输出中的值是多少。 – 2012-01-15 20:41:20

回答

6

我找到了解决方案,它是由不正确的基础URL设置引起的。

我基地的URL,然后为:$config['base_url'] = 'localhost/CodeIgniter';

现在我把它改成

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

,现在工作得很好。

感谢谁试图帮助:)

0

只是一个建议,可能是它可以帮助你:

$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端口,domain,port number(例如8080,8880等)和codeigniter root folder。它也可以在你的任何codeigniter项目中重用。 :)

您还可以查询this article

相关问题