0

我是 CodeIgniter 的新手。我只是在想,有什么方法可以将任何变量发送到控制器的构造函数,就像我在 Java 中创建对象时可以做的一样?

4

2 回答 2

1

您可以通过 URL 将变量发送到控制器函数。

例如,如果您的 URL 是 www.domain.com/index.php/reports/userdata/35,那么文件 controllers/reports.php 中的控制器函数将如下所示:

function userdata($userId) {
    .....
}
于 2013-02-25T20:46:14.163 回答
0

我不知道您为什么要这样做以及您打算从哪里获取要发送的变量,但这在这种情况下确实有效:

function __construct($f=null) {
    parent::__construct();

    if($f){
       return $f; //Here use the variable for whatsoever you want.
    }
}

function testvariable($id) { //Using $id, you could still get the value from url

    $myVariable = 3; //Or you could just hard code the value

    if($id){
    $myVariable = $id;
}

    echo $this->__construct($myVariable);
    exit;
}

当你跑http://localhost/controller/testvariable/54

你会得到结果 54

当你跑http://localhost/controller/testvariable

你会得到结果 3

在这些之外,另一个选择是在构造中定义变量。

于 2013-02-25T22:51:43.547 回答