代码之家  ›  专栏  ›  技术社区  ›  Glavić

采用zend框架的模块化网站,操作堆栈

  •  3
  • Glavić  · 技术社区  · 17 年前

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

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

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

    谢谢。

    附笔

    附言#2

    我已经找到了答案,下面列出了答案。如果有什么我可以更容易地做到的,请写下来。

    3 回复  |  直到 13 年前
        1
  •  1
  •   adepretis adepretis    17 年前

    您可以使用 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/somecontroller/right , /某个模块/某个控制器/中心 它们最终出现在相应的布局段中。

        2
  •  1
  •   Glavić    17 年前

    我在其他论坛上找到了答案。以下是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'));
        }
    
    }
    

    基站控制器 ,每个控制器都扩展

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

    布局 .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>
    

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

        3
  •  0
  •   guny    15 年前

    嗨,我也遇到了同样的问题。你建议的解决方案效果很好。但我的baseController位于模块库中。代码在baseController上运行顺畅,但当我在另一个模块中扩展控制器时,由于baseController无法在其他控制器中识别,会发生错误 例如: 模块/基座/控制器/基座控制器

    有任何解决方案吗?