为什么我的控制器在直接调用时工作正常但在从另一个控制器调用时没有响应(尽管动作代码似乎执行正常)?下面是代码示例。
// 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;
}
}
答案 0 :(得分:2)
Request::execute()返回一个Response对象。你可能跟着一个过时的教程,$request->response
它是如何在以前的(3.1?)次要版本中工作的。
这应该有效:
$response = Request::factory('bar')->execute();
$this->template->bar = $response->body();