Quantcast
Channel: CTRL-F5 » PHP
Viewing all articles
Browse latest Browse all 10

Zend Framework ContextSwitch

$
0
0

Today was the day I got tired of constantly disabling the layout and viewRenderer when making ajax calls in the ZF project I’m currently working on.
Today is the day I went and visited my old friend, the Zend Framework ContextSwitch Action Helper.

I have used the contextSwitch before, but it was some time ago, and once setup you don’t have to look at it that much anymore, so I guess that’s why I forgot how it worked…
A quick reminder is to follow, including some caveats I encountered while doing the setup.

First things first, initialising the contextSwitch, which I did in a base controller:

abstract class BaseController extends \Zend_Controller_Action
{
    /**
     * Helper to get better code completion
     *
     * @var $conSwitch \Zend_Controller_Action_Helper_ContextSwitch
     */
    protected $contextSwitch = null;

    public function init()
    {
        parent::init();

        $this->contextSwitch = $this->_helper->contextSwitch();
        $this->contextSwitch
                ->setContext('html', array(
                    'suffix' => ''
                ))
                ->setAutoDisableLayout(true)
                ->setAutoJsonSerialization(true)
                ->setDefaultContext('html')
                ->initContext();
    }
}

Fairly easy, or so it seems. There are some oddities in the contextSwitch I was struggling with…

HTML context

For starters, there is no default html context available. This is very odd, since the framework is build to generate HTML pages…
This was easily overcome by just adding it. Note the use of setContext() rather than addContext(), this ensures the context is overwritten when it should exist in the future (after ZF update or someone adds it in a superclass).
I’m also setting an empty suffix, so the default .phtml files will be rendered when using the html context, if I were to set this to ‘html’, the viewRenderer would look for ‘my-action.html.phtml’

setDefaultContext()

Now this one is weird, I actually do not know if this one does anything at all… When in a sub-classed controller action method, which was requested without a ‘format’ get parameter, and I execute the following command:

$this->contextSwitch->getCurrentContext();

It just returns NULL…

Adding action contexts

When I put the above code to use, it was not working. Well, when performing an ajax request to an action, even with the ‘format’ GET parameter set to ‘html’, the contextSwitch did not disable the layout for instance.
To resolve this I added a context for the action I was testing, explicitly:

//in the correct controller
public function init()
{
    parent::init();

    $this->contextSwitch
            ->addActionContext('index', array('html'));
}

Good to go , right? hmm, wrong… Still did not do what I expected it to do, so alt-tabbed and ctrl-T to Google. The solution?

//in the correct controller
public function init()
{
    parent::init();

    $this->contextSwitch
            ->addActionContext('index', array('html'))
            ->initContext(); //init again!!!
}

init again!!!


Viewing all articles
Browse latest Browse all 10

Trending Articles