Plootor
Plootor

Reputation: 61

How to add a context to whole controller in zendFramework

there are posibility to add for example json context to specific action:

$this->_helper->ajaxContext()
    ->addActionContext('index', 'json')
    ->initContext();

but how about if I want to add jsonContext to two or all action in current controller; I tryed:

$this->_helper->ajaxContext()
    ->addActionContext(array('index', 'second'), 'json')
    ->initContext();

but without result. I know I can use:

$this->_helper->ajaxContext()
   ->addActionContext('index', 'json')
   ->initContext();
$this->_helper->ajaxContext()
   ->addActionContext('second', 'json')
   ->initContext();

but I am looking more original solution. Thank you in advance.

Upvotes: 0

Views: 921

Answers (3)

Tim Riemenschneider
Tim Riemenschneider

Reputation: 385

To add the context to ALL actions, you can place this into the init of your controller:

$contextSwitch = $this->_helper->getHelper('contextSwitch');
$action = $this->getRequest()->getActionName();
$contextSwitch->addActionContext($action, 'pdf')
              ->initContext();

This works as long as you don't use forward or redirect, since it adds the context to the current action.

Upvotes: 1

Eric MORAND
Eric MORAND

Reputation: 6786

I know this is an old question, but in case someone else is looking for a solution, I think subclassing Zend_Controller_Action_Helper_ContextSwitch is the way to go.

In my case, I subclassed it so that it considers "*" as a wildcard for "all actions" :

class My_Controller_Action_Helper_ContextSwitch extends Zend_Controller_Action_Helper_ContextSwitch {
/**
 * Adds support logic for the "*" wildcard.
 * 
 * @see Zend_Controller_Action_Helper_ContextSwitch::getActionContexts()
 */
public function getActionContexts($action = null) {
    $parentContexts = parent::getActionContexts($action = null);

    $contextKey = $this->_contextKey;
    $controller = $this->getActionController();

    if (isset($controller->{$contextKey}['*'])) {
        $contexts = $controller->{$contextKey}['*'];
    }
    else {
        $contexts = array();
    }

    return array_merge($parentContexts, $contexts);
}

/**
 * Adds support logic for the "*" wildcard.
 *
 * @see Zend_Controller_Action_Helper_ContextSwitch::hasActionContext()
 */
public function hasActionContext($action, $context) {       
    if (!$result = parent::hasActionContext($action, $context)) {
        $controller = $this->getActionController();
        $contextKey = $this->_contextKey;

        $contexts = $controller->{$contextKey};

        foreach ($contexts as $action => $actionContexts) {
            foreach ($actionContexts as $actionContext) {
                if ($actionContext == $context && $action == '*') {
                    return true;
                }
            }
        }
    }

    return $result;
}

}

And in my controller, I use the following syntax to set-up the context switch :

$contextSwitch = $this->_helper->getHelper('contextSwitch');
    $contextSwitch
        ->addActionContext('*', array('help'))
        ->initContext()
    ;

By doing this, the "help" context is available to every actions in my controller.

These samples have not been fully tested and are certainly not perfect, but they are a good starting point to solve the problem.

Upvotes: 2

markus
markus

Reputation: 40675

Well, your second version is wrong and your third version is overkill.

This is how I usually do it:

$this->_helper->ajaxContext()
   ->addActionContext('index', 'json')
   ->addActionContext('second', 'json')
   ->initContext();

If that is not enough for you, you could loop through all actions and add them to the context.

Upvotes: 1

Related Questions