duanjihe5180 2012-06-06 11:41 采纳率: 100%
浏览 25
已采纳

Kohana 3.x call_user_func()错误

i keep on having this error message on Kohana 3

ErrorException [ Warning ]: call_user_func() expects parameter 1 to be a valid callback, function 'pages_save' not found or invalid function name

inside my controller i have the following codes:

 public function action_pages(){
    return call_user_func('pages_'.$this->request->param('id'));
}
function pages_save(){
    $this->auto_render = false;
}

if i would access admin/pages/save i should be redirected to pages_save() function right? but somehow i cant? Kohana keeps on throwing me the following exception stated above. how should i go with this. optimizing is one thing in my mind, if i do switch-case / if-else block it would take me forever if there are too many that i need to do...

  • 写回答

2条回答 默认 最新

  • douxiongzhen2126 2012-06-06 12:57
    关注

    First of all you should trust the error message. Which means you did an error. Clean up your code a little:

    public function action_pages()
    {
        $method = sprintf('pages_%s', $this->request->param('id'));
        return call_user_func($method);
    }
    
    public function pages_save()
    {
        $this->auto_render = false;
    }
    

    The changes here are only little: Added the missing visibility specifier to pages_save and put the methodname into a variable called $method.

    Now it's getting more visible, that you used a public function name here which is not defined, instead of the class method you'd like to invoke on $this. So let's fix that:

    public function action_pages()
    {
        $method = sprintf('pages_%s', $this->request->param('id'));
        return $this->$method();
    }
    

    This should solve your issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计