在zend框架中实现动作堆栈的意外输出。actionstack的正确流程是什么或它是如何工作的?

时间:2010-12-03 06:26:12

标签: zend-framework

动作堆栈工作与下面的代码罚款,但问题是输出出现意外的模式..在代码的最后解释我得到的输出和期望的输出。第一次使用动作堆栈我不知道请求如何处理

        MY Plugin Class
    -----------------------
    class My_Plugins_ActionStack extends Zend_Controller_Plugin_Abstract
    {

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {

                    $action_stack = new Zend_Controller_Action_Helper_ActionStack();
                    $action_stack->actionToStack('index', 'index', 'default', array('position' => 'right'));
                    $action_stack->actionToStack('index', 'index', 'user', array('position' => 'left'));
                    $action_stack->actionToStack('index', 'index', 'hr', array('position' => 'center'));

    }
    Base Controller
    ------------------

    class My_Controller_Base extends Zend_Controller_Action
    {
     public function preDispatch()
        {

           $position = $this->_request->getParam('position', false);


            if ($position) {
                    $this->_helper->viewRenderer->setResponseSegment($position);
            }


        }


        public function init()
        {


        }
    }
    Default Controller
    ---------------------------
    class Default_IndexController extends Mlive_Controller_Base
    {



        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {

        }


    }
    index.phtml in default controller
    ============
    This is default module
    HR Controller
    -----------------
    class Hr_IndexController extends Mlive_Controller_Base
    {

        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {
        }


    }
    index.phtml in hr controller
    ============
    this is hr module
    User Controller
    ----------------------
    class User_IndexController extends Mlive_Controller_Base
    {

        public function init()
        {
            /* Initialize action controller here */
        }

        public function indexAction()
        {

        }


    }
    index.phtml in user controller
    ============
    This is user module
    -----------------------------------------------
    And finally the layout looks like

    Layout.phtml
    ------------------------
    This is basic layout
    <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>
    <div>

OUTPUT 
---------------------------
 Output  for default module
 --------------------------
 This is basic layout
LEFT:
This is user module
CENTER:
RIGHT:
this is hr moduleThis is user module
Content:
This is default module


-----------------------------------------------
Output for user module
------------------------------------------------
This is basic layout
LEFT:
this is hr module
CENTER:
RIGHT:
this is hr moduleThis is default module
Content:
This is user module
---------------------------------
Output for hr module
-----------------------------------
This is basic layout
LEFT:
This is user module
CENTER:
RIGHT:
this is hr moduleThis is default module
Content:
this is hr module

DESIRED OUTPUT
----------------
---------------------------
 Output  for default module
 --------------------------
 This is basic layout
LEFT:
This is user module
CENTER:
this is hr module
RIGHT:
This is default module
Content:
This is default module


-----------------------------------------------
Output for user module
------------------------------------------------
 This is basic layout
LEFT:
This is user module
CENTER:
this is hr module
RIGHT:
This is default module
Content:
This is user module
---------------------------------
Output for hr module
-----------------------------------
 This is basic layout
LEFT:
This is user module
CENTER:
this is hr module
RIGHT:
This is default module
Content:
this is hr module

3 个答案:

答案 0 :(得分:1)

要回答ActionStack如何运作?

动作堆栈以后进先出顺序执行您的操作。因此,在您的示例中,您按顺序添加模块:默认,用户,HR - 它们将按HR,用户,默认,您的请求操作的顺序执行。 您的请求操作默认情况下首先存在于堆栈中,并执行最后

你的输出可能不像预期的那样,因为你的'位置'参数在堆栈上的最后一个动作运行时被设置,但在你的实际动作运行之前 - 它被卡在'正确'上 - 所以你的动作的所有输出被附加到该段。

正如其他地方所述,动作堆栈很重且很慢,因为它们会为dispatchLoop进程添加大量额外的迭代。

执行您要执行的操作的更好方法是使用placeholderspartials以及将您想要的一些常用代码移动到公共库中。

答案 1 :(得分:0)

Actionstack应始终为avoided,因为每次请求它都会通过调度循环,这会产生大量开销。

在视图中使用部分视图与$this->partial()$this->render(),或者如果您需要自定义功能,请使用View Helpers

答案 2 :(得分:0)

如果仅使用同一控制器中的操作,则可以使用:

public function someAction() 
{
    $this->defaultAction('right');
    $this->userAction('center');
    $this->hrAction('left');
}

public function defaultAction($position)
{
    // your code
}

但更好的是行动助手或服务 对于多个模块,您可以使用侦听器或适配器。

然而,看起来你正在努力实现只需切换布局的事情。