开发者

Unexpected output in implementing action stack in zend framework.What is the correct flow of actionstack or how does it work?

开发者 https://www.devze.com 2023-01-28 08:17 出处:网络
The action stack work fines with the below code but the problem is thatthe output came in a unexpected patter.. At the end of the codeexplain the output i am getting and what was the desired out put .

The action stack work fines with the below code but the problem is that the output came in a unexpected patter.. At the end of the code explain the output i am getting and what was the desired out put . Using the action stack for first time i dont know how the request processed

        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


To answer How does ActionStack work?

Action stack executes your actions in the last-in first-out order. So in your example you add modules in order: Default, User, HR - they will be executed in the order HR, User, Default, Your Request Action. Your Request Action already exists on the stack first by default and gets executed last.

Your output is probably not as expected because your 'position' param that gets set when the last action on the stack runs, but before your real action gets run - it's getting stuck on 'right' - so all output from your action is being appended to that segment.

As stated elsewhere, action stacks are heavy and slow as they add a lot of extra iterations to the dispatchLoop process.

A better way of doing what you are trying to do is to use placeholders and partials as well as moving some of common code that you want into a common library.


Actionstack should always be avoided because every time you request it it goes through the dispatch loop, which creates a lot of overhead.

Use Partial views with $this->partial() or $this->render() in your views instead or use View Helpers if you need custom functionality.


If you use actions only from the same controller, you may use:

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

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

But better would be action helper or service.
For multiple modules you could use listeners or adapters.

However, looks like you are trying to achieve things you could do just switching the layout.

0

精彩评论

暂无评论...
验证码 换一张
取 消