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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog