控制器不会在 Zend Framework 的视图中传递变量
Controller won't pass a variable in View for Zend Framework
所以我开始学习 Zend Framework 1 和 运行 解决一些问题,在视图中传递值。我在 IndexContoller 中创建了一个简单的变量,如下所示:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->content = "Lorem ipsum";
}
}
然后我在这样的布局中调用它:
<div class="carousel-caption">
<h2><?php echo $this->layout()->content; ?></h2>
</div>
它 returns 没什么,当我做 var dump
var_dump($this->layout()->content)
我回来了:
string(0) ""
如何解决这个问题?
此 $this->layout()->content
应在您的布局中使用,以呈现在您的操作特定视图中生成的输出。通常许多特定于操作的 .phtml
文件将使用 common layout.
如果您想将某些内容从控制器传递到视图,您可以在控制器上使用 $this->foo = 'bar'
然后在您的视图中使用 echo $this->foo
;
渲染它
我还发现这个现有的 post 可能会回答您的问题:Sending variables to the layout in Zend Framework - 它解释了布局和视图的重叠和共性。如果你觉得这是一个骗局,由你决定......
ZF1 使用变量content
来捕获视图的结果并传递给布局。我猜你的 index.phtml
视图是空的,这就是你从 $this->layout()->content
得到空字符串的原因。
如果您想将一些数据传递给布局,请照常将其分配给视图对象,但可以将其命名为 'content':
public function indexAction()
{
$this->view->foo = "Lorem ipsum";
}
然后在您的布局中,像访问普通视图变量一样访问它:
<div class="carousel-caption">
<h2><?php echo $this->foo; ?></h2>
</div>
我同意 ficuscr 的回答。
但是,如果您真的需要从控制器设置某些布局的值,这个丑陋的 hack 可能适合您:
$this->_helper->layout()->foo = "value";
所以我开始学习 Zend Framework 1 和 运行 解决一些问题,在视图中传递值。我在 IndexContoller 中创建了一个简单的变量,如下所示:
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->content = "Lorem ipsum";
}
}
然后我在这样的布局中调用它:
<div class="carousel-caption">
<h2><?php echo $this->layout()->content; ?></h2>
</div>
它 returns 没什么,当我做 var dump
var_dump($this->layout()->content)
我回来了:
string(0) ""
如何解决这个问题?
此 $this->layout()->content
应在您的布局中使用,以呈现在您的操作特定视图中生成的输出。通常许多特定于操作的 .phtml
文件将使用 common layout.
如果您想将某些内容从控制器传递到视图,您可以在控制器上使用 $this->foo = 'bar'
然后在您的视图中使用 echo $this->foo
;
我还发现这个现有的 post 可能会回答您的问题:Sending variables to the layout in Zend Framework - 它解释了布局和视图的重叠和共性。如果你觉得这是一个骗局,由你决定......
ZF1 使用变量content
来捕获视图的结果并传递给布局。我猜你的 index.phtml
视图是空的,这就是你从 $this->layout()->content
得到空字符串的原因。
如果您想将一些数据传递给布局,请照常将其分配给视图对象,但可以将其命名为 'content':
public function indexAction()
{
$this->view->foo = "Lorem ipsum";
}
然后在您的布局中,像访问普通视图变量一样访问它:
<div class="carousel-caption">
<h2><?php echo $this->foo; ?></h2>
</div>
我同意 ficuscr 的回答。
但是,如果您真的需要从控制器设置某些布局的值,这个丑陋的 hack 可能适合您:
$this->_helper->layout()->foo = "value";