将多个URL函数映射到同一视图

时间:2013-02-18 17:06:21

标签: php codeigniter

在使用codeigniter时,我有一个退出页面的控制器。我希望用户能够访问网址site.com/example/copy,site.com/example/revise,每次加载相同的网页并传递不同的变量,如下所示:

class example extends Secure_Controller {

    public function revise()
    {
        $id = $_GET["id"];
        $show_for_edit("revise", $id);
    }

    public function copy()
    {
        $id = $_GET["id"];
        $show_for_edit("copy", $id);
    }

    public function show_for_edit($edit_type, $id)
    {
        //A lot of database queries that I don't
        //want duplicated under revise() and copy()
        $data["id"] = $id;
        $data["edit"] = $edit_type;
        $this->load->view('model', $data);
    }
}

但我收到错误:

  

致命错误:调用未定义的函数show_for_edit()

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

在PHP中,当调用该方法的对象没有引用时,会发生此错误,即它为null。

在您的情况下,调用方法“show_for_edit”的对象为null,因此看起来是错误。

检查调用此方法的变量,并确保它不为null,并且是“example”类的实例。

此外,我不确定这些来电$show_for_edit ("check", $id)$show_for_edit ("copy", $id)是否正确。我认为他们应该是:

$this-> show_for_edit ("check", $ id);

$this-> show_for_edit ("copy", $ id);