Sunday, April 3, 2011

Zend Framework 'AjaxContent' & ResponseSegmentation problem

I'm using jquery in Zend Framework, it's my first trial. I've already found out through another question, that I can change the response by changing the context like so:

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('myaction', 'html');
$ajaxContext->initContext();

Now this has helped a lot but a new problem has shown:

My page consists of different responseSegments and when I responde to an Ajax request by changing the Context, my other ResponseSegments also 'think' they are sending Ajax but they're not. The front controller asks for a viewscript.ajax.phtml... which is wrong, it should be viewscript.phtml (exists).

From stackoverflow
  • In the meantime I figured out how to solve this and because I think others will encounter the same problem in the future, I will answer my own question here:

    In my ActionSetup.php (or bootstrap.php if the action setup is not separated) I needed to make sure that actions are only pushed to the action stack, if the request was no XmlHttpRequest.

    The only thing that was missing was an if statement:

    if (!$request->isXmlHttpRequest())
    

    The whole thing looks like that:

    /**
     * Front Controller plugin to set up the action stack.
     *
     */
    class Project_Controller_Plugin_ActionSetup extends Zend_Controller_Plugin_Abstract
    {
        public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
        {
            if (!$request->isXmlHttpRequest())
            {
                $front = Zend_Controller_Front::getInstance();
                if (!$front->hasPlugin('Zend_Controller_Plugin_ActionStack'))
                {
                    $actionStack = new Zend_Controller_Plugin_ActionStack();
                    $front->registerPlugin($actionStack, 97);
                } else
                {
                    $actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
                }
    
                $menuAction = clone ($request);
                $menuAction->setActionName('menu')
                ->setControllerName('index');
                $actionStack->pushStack($menuAction);
    
                $userlogAction = clone ($request);
                $userlogAction->setActionName('userlog')
                ->setControllerName('index');
                $actionStack->pushStack($userlogAction);
    
               //etc.
            }
        }
    }
    

0 comments:

Post a Comment