hi frnds有人能清楚地向我解释一下使用PHP的codeIgniter中_remap函数的用途吗。。。
请参阅此处
重新映射函数调用
如果你的控制器包含一个名为_remap()的函数,那么无论你的URI包含什么,它都会被调用。它覆盖了URI确定调用哪个函数的正常行为,允许您定义自己的函数路由规则。
例如:你的url是localhost/index.php/user/index
,并且你不想为此调用index
,那么你可以使用_remap()
来映射新函数view
,而不是像这样的index
。
public function _remap($method)
{
if ($method == 'index')
{
$this->view();
}
else
{
$this->default_method();
}
}
在这段简短的代码中,我们必须在url中传递索引,以显示我们所响应的内容。但是函数本身内部有_remap()。我们应该通过指数而不是指数。假设你有很多函数,你刚刚将其命名为公共函数codeig(){},在url中传递codeig是一种耻辱,对吧?。因此,您将使用_remap并将codeig设置为hello或any(您想要的函数名),现在您可以将hello或您为函数命名的内容传递给url。
class Tuts extends CI_Controller{
public function index($name = 'john',$age=18){
echo "Your name is $name, you are $age years old";
}
public function _remap($method){
if ($method === 'indexs') { //you should have to type indexs in uri instead of index
$this->index();
}else{
$this->default_method();
}
}