为什么我的Kohana 3.3控制器在从另一个控制器中调用时响应为NULL?

时间:2014-02-04 02:20:20

标签: php kohana kohana-3.3

为什么我的控制器在直接调用时工作正常但在从另一个控制器调用时没有响应(尽管动作代码似乎执行正常)?下面是代码示例。


// This contoller works ok when called directly
// with a browser but its response is null when
// it is called from within another controller
// as it is meant to be (though the action
// code seems to be executed ok in both cases)
class Controller_Bar extends Controller_Template {

    public $template = 'bar';

    public function action_index()
    {
        $items = ... // this gets populated with ORM

        $this->template->items = $items;
    }
}


// This contoller is meant to be called directly
// with a browser and seems to work ok itself
// but the controller it calls inside responds
// with NULL in this case
class Controller_Foo extends Controller_Template {

    public $template = 'foo';

    public function action_index()
    {

        // I expected $barResponse to become a rendered 'bar' view
        // which is meant to be a segment to be merged into the
        // 'foo' view which becomes the resulting web page
        $barResponse = Request::factory('bar')->execute()->response;

        // but this writes NULL to the log
        Log::instance()->add(Log::NOTICE, gettype($barResponse) );

        $this->template->yoo = 'zzz'; // this works ok

        $this->template->bar = $barResponse;
    }
}

1 个答案:

答案 0 :(得分:2)

Request::execute()返回一个Response对象。你可能跟着一个过时的教程,$request->response它是如何在以前的(3.1?)次要版本中工作的。

这应该有效:

$response = Request::factory('bar')->execute();

$this->template->bar = $response->body();