关于php:带有zend框架的模块化网站,操作堆栈

Modular web site with zend framework, stack of actions

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

1
2
3
4
5
6
7
   <?= $this->layout()->left_container ?>


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


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

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

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

谢谢。

p.s.

adepretis的代码与我的相似,我的操作视图在布局内运行,这意味着当发生错误时,它将打印在调用该操作的布局中。在布局输出之前,没有乳清的动作已生成吗?另一个不好的事情是,在每一个动作中我都必须运行...-> setResponseSegment,我希望这是自动化的。

p.s。 #2

我找到了答案,下面将其列为答案。如果有乳清,我可以更轻松地完成此操作,请写下来。


我在其他论坛上找到了答案。这是请求者:

MyPlugin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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,每个控制器都扩展

1
2
3
4
5
6
7
8
9
10
11
12
class BaseController extends Zend_Controller_Action
{

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

}

Layout.phtml

1
2
3
4
5
6
7
8
9
10
    <u>LEFT:</u>
    <?=$this->layout()->left?>


    <u>CENTER:</u>
    <?=$this->layout()->center?>


    <u>RIGHT:</u>
    <?=$this->layout()->right?>

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


您可以使用ActionStack帮助器。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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最终出现在对应的布局段中。


嗨,我也遇到同样的问题。您建议的解决方案工作正常。但是我的baseController在模块base中。代码可以与baseController一起正常工作,但是当我在另一个模块中使用控制器扩展时发生错误,因为base Controller无法在其他控制器中识别
例如:
模块/基础/控制器/基础控制器
模块/用户/控制器/ userController

任何解决方案吗?