dongzhang1875 2015-01-22 09:03
浏览 66
已采纳

来自Appcontroller的Cakephp-Calling Parent render()函数给出错误:在非对象上调用成员函数send()

I am trying to build a piece of application using cakephp; which will generate output in multiple formats (xml, json, custom, html).

Based on certain property, output format will be decided. What I want is, if html is output ype; then I want application to render view as usual (Regular Controller->render); else data should be rendered in other formats

Here is what I am trying to do. I have overridden function render in AppController.php as following -

public function render($view = null, $layout = null) {
    if ($this->rType == "json") {
        $this->_renderJson();
    } else if ($this->rType == "xml") {
        $this->_renderXml();
    } else if ($this->rType == "custom") {
        $this->_renderCustom();
    } else {
        parent::render($view,$layout);
    }
}

This works perfectly with other formats but html.

I want call should be forwarded to Controller::render as normal cakephp flow would do. Instead it gives me below error

Error: Call to a member function send() on a non-object File: /xx/lib/Cake/Routing/Dispatcher.php Line: 174

Any thoughts - how can I resolve this ?

  • 写回答

2条回答 默认 最新

  • douzhou7656 2015-01-22 09:32
    关注

    When overwriting things you must make sure that your overriden method matches the original implementation with regards to the arguments that it takes and values that it returns.

    Your render() method lacks the proper return value, which should be an instance of CakeResponse, which is used later on by the dispatcher.

    https://github.com/cakephp/cakephp/blob/2.6.0/lib/Cake/Controller/Controller.php#L930

    https://github.com/cakephp/cakephp/blob/2.6.0/lib/Cake/Routing/Dispatcher.php#L174

    So, change your custom rendering methods to return $this->response, and add appropriate returns in the overwritten render() method:

    public function render($view = null, $layout = null) {
        if ($this->rType == "json") {
            return $this->_renderJson();
        } else if ($this->rType == "xml") {
            return $this->_renderXml();
        } else if ($this->rType == "custom") {
            return $this->_renderCustom();
        } else {
            return parent::render($view,$layout);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛