将参数传递给codeigniter控制器中的索引函数,给出错误

时间:2013-11-19 22:58:16

标签: php codeigniter

我正在使用codeigniter latest version。我制作了一些控制器,每当我想通过URL传递参数到index()函数时,它就会给出错误信息。但如果函数不是index(),它的效果非常好。这就是我试图实现这一目标的方式。

My URL:  localhost/ci_project/index/argument

class Index extends CI_Controller{

 public function __construct(){
    parent::__construct();
 }
 public function index($arg = null){
      echo $arg;                            // ERROR 404 Page not found
     $this->load->view('index',array('data'=>null));
 }
 public function other($arg = null){
    echo $arg;
    $this->load->view('other',array('data'=>null));

 }

}

URL: localhost/ci_project/other/argument工作正常,但如果我将控制器other替换为index,则会显示错误消息404 Page not found。有什么建议?提前谢谢。

如果单击以下链接,则应传递参数。

<a href="<?php echo base_url();?>index/argument">Click to pass an argument</a>

3 个答案:

答案 0 :(得分:1)

首先,回复你的网址:

<a href="<?php echo base_url(); ?>/index/argument">Click to pass an argument</a>
----------------------------------^

我刚用这个Html创建了一个模拟测试:

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <div><a href="<?php echo base_url('tester'); ?>/index/argument">Click to pass an argument</a></div>
    </body>
</html>

和这个PHP代码:

class tester extends CI_Controller{

function __construct() {
    parent::__construct();

}

public function index($arg = 'null'){
    var_dump($arg);
    $this->load->view('tester');
  }
}

结果是:

string(8) "argument"
Click to pass an argument

答案 1 :(得分:0)

这里你的类名是索引,函数名也是索引,因此PHP认为你的函数索引是一个构造函数,因为函数名与类名相同,因此你遇到了问题

答案 2 :(得分:-1)

尝试

<?php echo base_url();?>/Index/index/argument

我在这里想说的是,将其传递给www.website.com/controller/function/argument

相关问题