0

たとえば、json コンテキストを特定のアクションに追加する可能性があります。

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

しかし、現在のコントローラーの 2 つまたはすべてのアクションに jsonContext を追加したい場合はどうでしょうか。私が試した:

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

しかし結果なし。私は使用できることを知っています:

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

しかし、私はより独創的な解決策を探しています。前もって感謝します。

4

3 に答える 3

2

これは古い質問であることは承知していますが、他の誰かが解決策を探している場合は、Zend_Controller_Action_Helper_ContextSwitch をサブクラス化するのがよいと思います。

私の場合、「*」を「すべてのアクション」のワイルドカードと見なすようにサブクラス化しました。

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;
}

}

私のコントローラーでは、次の構文を使用してコンテキスト スイッチをセットアップします。

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

これにより、コントローラー内のすべてのアクションで「ヘルプ」コンテキストを利用できるようになります。

これらのサンプルは完全にはテストされておらず、完全ではありませんが、問題を解決するための出発点としては適しています。

于 2012-09-19T07:38:27.850 に答える
1

ええと、あなたの2番目のバージョンは間違っていて、あなたの3番目のバージョンはやり過ぎです。

これは私が通常それをする方法です:

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

それだけでは不十分な場合は、すべてのアクションをループしてコンテキストに追加できます。

于 2011-10-03T07:08:00.977 に答える