具有 Zend 框架、动作堆栈的模块化网站

2021-12-29 00:00:00 php zend-framework

如何使用 Zend 框架构建模块化网站.我在数据库中有页面,每个页面都表示为 url.每页有1toN个内容.每个内容都有控制器、动作和位置(+其他现在不重要的列).因此,一个请求是一个页面和多个内容(多个操作).如何在输出之前构建所有操作?我想要像下面的示例那样的布局设计,其中内容放在容器中(在布局打印输出之前运行操作).

<?= $this->layout()->left_container ?>

<div id="中心"><?= $this->layout()->center_container ?>

<div id="右"><?= $this->layout()->right_container ?>

直到现在我从布局视图调用操作,但我不喜欢这种方法:

foreach ($contents as $item) {echo $this->action($item['action'], $item['controller'], null, array('content' => $item));}

谢谢.

附注

adepretis 的代码与我的类似,我的操作视图在布局内运行,这意味着当发生错误时,它会打印在调用操作的布局中.在布局输出之前构建动作是否没有乳清?另一件坏事是,我必须在每个操作中运行...->setResponseSegment,我希望这是自动化的.

ps.#2

我找到了答案,它列在下面作为答案.如果有乳清,我可以更容易地做到这一点,请写下来.

解决方案

我在其他论坛上找到了答案.答案如下:

我的插件

class MyPlugin 扩展 Zend_Controller_Plugin_Abstract{公共函数 routeStartup(Zend_Controller_Request_Abstract $request){$action_stack = new Zend_Controller_Action_Helper_ActionStack();//在这里,我将从 db 中读取操作并循环运行它,但例如很少静态添加波纹管$action_stack->actionToStack('index', 'content', 'default', array('position' => 'left'));$action_stack->actionToStack('index', 'content', 'default', array('position' => 'center'));$action_stack->actionToStack('index', 'edo', 'default', array('position' => 'center'));$action_stack->actionToStack('left', 'edo', 'default', array('position' => 'left'));$action_stack->actionToStack('right', 'edo', 'default', array('position' => 'right'));}}

BaseController,每个控制器都扩展

class BaseController 扩展 Zend_Controller_Action{公共函数 preDispatch(){$position = $this->_request->getParam('position', false);如果($位置){$this->_helper->viewRenderer->setResponseSegment($position);}}}

布局.phtml

<h2><u>左:</u></h2><?=$this->layout()->left?>

<div><h2><u>中心:</u></h2><?=$this->layout()->center?>

<div><h2><u>右:</u></h2><?=$this->layout()->对吗?>

这就是我想要的,如果有人有更好的解决方案,请回答这个问题,我会接受他的回答.

How to build modular web site with Zend framework. I have pages in db, every page is represented as url. Every page has 1toN contents. Every content has controller, action and position (+other now not important columns). So, one request is one page and multiple contents (multiple actions). How can I build all actions before the output? I would like to have layout design like example bellow, where contents are put in there containers (actions are run before layout print-out).

<div id="left">
   <?= $this->layout()->left_container ?>
</div>
<div id="center">
   <?= $this->layout()->center_container ?>
</div>
<div id="right">
   <?= $this->layout()->right_container ?>
</div>

Until now I called actions from layout view, but I do not like this approach:

foreach ($contents as $item) {
    echo $this->action($item['action'], $item['controller'], null, array('content' => $item));
}

Thanks.

p.s.

adepretis's code is similar to my, views of my actions are run inside layout, which means that when error occurres it is printed in layout where the action is called. Is there no whey that actions are build before layout output? Another bad thing is that in every action I must run ...->setResponseSegment, I would like this to be automated.

p.s. #2

I have found answer, it is listed bellow as answer. If there is a whey I can do this easier please write it down.

解决方案

I found my answer on other forum. Here is the asnwer:

MyPlugin

class MyPlugin extends Zend_Controller_Plugin_Abstract
{

    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
        $action_stack = new Zend_Controller_Action_Helper_ActionStack();
        // here I will read actions from db and run it in loop, but for example few are staticly added bellow
        $action_stack->actionToStack('index', 'content', 'default', array('position' => 'left'));
        $action_stack->actionToStack('index', 'content', 'default', array('position' => 'center'));
        $action_stack->actionToStack('index', 'edo', 'default', array('position' => 'center'));
        $action_stack->actionToStack('left', 'edo', 'default', array('position' => 'left'));
        $action_stack->actionToStack('right', 'edo', 'default', array('position' => 'right'));
    }

}

BaseController, that every controller extends

class BaseController extends Zend_Controller_Action
{

    public function preDispatch()
    {
        $position = $this->_request->getParam('position', false);
        if ($position) {
            $this->_helper->viewRenderer->setResponseSegment($position);
        }
    }

}

Layout.phtml

<div>
    <h2><u>LEFT:</u></h2>
    <?=$this->layout()->left?>
</div>
<div>
    <h2><u>CENTER:</u></h2>
    <?=$this->layout()->center?>
</div>
<div>
    <h2><u>RIGHT:</u></h2>
    <?=$this->layout()->right?>
</div>

This is what I wanted, if anyone has a better solution please answer the question and I will accept his answer.

相关文章