如何使用 Zend 框架构建模块化网站。我在数据库中有页面,每个页面都表示为 url。每页都有 1 到 N 的内容。每个内容都有 Controller 、 Action 和位置(+其他现在不重要的列)。因此,一个请求是一页和多个内容(多个操作)。如何在输出之前构建所有操作?我想要布局设计,例如下面的示例,其中将内容放在容器中(操作在布局打印输出之前运行)。
<div id="left">
<?= $this->layout()->left_container ?>
</div>
<div id="center">
<?= $this->layout()->center_container ?>
</div>
<div id="right">
<?= $this->layout()->right_container ?>
</div>
直到现在我都从布局 View 调用操作,但我不喜欢这种方法:
foreach ($contents as $item) {
echo $this->action($item['action'], $item['controller'], null, array('content' => $item));
}
谢谢。
附注
adepretis 的代码与我的类似,我的操作 View 在布局内运行,这意味着当发生错误时,它会打印在调用操作的布局中。是否没有在布局输出之前构建操作的乳清?另一件坏事是,在我必须运行的每个操作中...->setResponseSegment,我希望它是自动化的。
附注#2
我找到了答案,下面列出了答案。如果有乳清,我可以更轻松地完成此操作,请写下来。
最佳答案
您可以使用 ActionStack helper .例如:
class MyController_Action extends Zend_Controller_Action {
function init() {
/** you might not want to add to the stack if it's a XmlHttpRequest */
if(!$this->getRequest()->isXmlHttpRequest()) {
$this->_helper->actionStack('left', 'somecontroller', 'somemodule');
$this->_helper->actionStack('center', 'somecontroller', 'somemodule');
$this->_helper->actionStack('right', 'somecontroller', 'somemodule');
}
}
class MyController extends MyController_Action {
function indexAction() {
// do something
}
}
class SomecontrollerController extends MyController_Action {
function leftAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('left_container');
}
function centerAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('center_container');
}
function rightAction() {
// do something
$this->_helper->viewRenderer->setResponseSegment('right_container');
}
}
对 /somemodule/my/index 的请求导致执行 /somemodule/somecontroller/left。 /somemodule/somecontroller/right, /somemodule/somecontroller/center 最终出现在相应的布局段中。
关于php - 具有 zend 框架、操作堆栈的模块化网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/583636/