我的问题是Kohana只提供了观点。当我
在Controller_Other中 View::factory('/foo/bar')
,它首先没有击中Controller_Foo。我想让它击中控制器,然后渲染视图。
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
$this->template->body = View::factory('/foo/bar');
}
}
我如何让它首先通过控制器运行:
class Controller_Foo extends Controller_Template {
public function action_bar() {
$this->myVar = 'foo';
}
}
因此,在视图中,当我从其他$myVar
调用views/foo/bar.php
时,View::factory()
始终设置在action_bar
中?
编辑:
必须有一种更简洁的方法,而不是强迫$foo = new Controller_Foo($this->request, $this->response);
$this->template->body = $foo->action_bar();
将自己的视图渲染为字符串然后继续:
{{1}}
答案 0 :(得分:2)
我不确定你在做什么 - 想要绑定全局视图变量或执行内部请求。 无论如何,以下是两种情况的例子:
绑定全局视图变量
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
View::bind_global('myVar', 'foo');
//binds value by reference, this value will be available in all views. Also you can use View::set_global();
$this->template->body = View::factory('/foo/bar');
}
}
执行内部请求
这是'foo / bar'动作
class Controller_Foo extends Controller_Template {
public function action_bar() {
$myVar = 'foo';
$this->template->body = View::factory('/foo/bar', array('myVar' => $myVar);
}
}
class Controller_Other extends Controller_Template {
public $template = 'main';
public function action_index() {
$this->template->body = Request::factory('foo/bar')->execute()->body();
//by doing this you have called 'foo/bar' action and put all its output to curent requests template body
}
}
答案 1 :(得分:0)
您应该始终将$ myVar变量传递给view,然后再将其渲染为
public function action_index() {
$this->template->body = View::factory('/foo/bar')->set('myVar', 'foo');
}
在其他控制器中,您应该再次设置它,因为View只是一个temlate。如果您打算在不同的脚本位置使用相同的视图,您可以将View实例分配给某个变量并在以下位置使用它:
public function action_index() {
$this->view = View::factory('/foo/bar')->set('myVar', 'foo');
$this->template->body = $this->view ;
}
public function action_bar() {
$this->template->head = $this->view ;
}