「アプリケーション/ビュー/ヘルパー」内にあるいくつかのビューヘルパーを無効にする方法を作ろうとしています...
私が本当に望んでいるのは、いくつかのヘルパーを有効または無効にするために、application.ini にいくつかのオプションを追加することです。
application.ini の例:
helpers.Helper1=on
helpers.Helper2=off
問題は、ヘルパーがオフのときに、ビューで別の結果を返すために、このヘルパーのいくつかの関数を書き直したいということです。このように、ビュー スクリプトで何も変更する必要はありません。
私は、ヘルパーごとに 2 つの異なる php ファイルを異なる場所に配置することを考えました。1 つは実際のヘルパーを使用し、もう 1 つは変更されたヘルパーを使用します (application.ini でオフになっているときに機能するように)。
問題は、どのビューをロードするかをビューに伝える方法がわからないことです...
どうすればそれができるか知っている人はいますか?
最終コード
わかりました、何度も試した後、次のコードで動作するようにしました:
ブートストラップ
protected function _initConfigureHelpers(){
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath("./../library/ConfigHelpers","Configurable_Helper");
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer'
);
$viewRenderer->setView($view);
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_ViewPlugins());
return $view;
}
Application_Plugin_ViewPlugins
class Application_Plugin_ViewPlugins extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request){
$front=Zend_Controller_Front::getInstance();
$bootstrap=$front->getParam('bootstrap');
$options=$bootstrap->getOption("helpers");
if (is_array($options)){
$view = $bootstrap->getResource('view');
foreach($options as $option => $value){
$helper=$view->getHelper($option);
if ($helper){
if ($value=="off")
$helper->__disable();
else if ($value!="on")
throw new Exception('The value of helpers.'.$option.' must be "on" or "off" on application.ini.');
} else {
throw new Exception("Inexistent Helper");
}
}
}
}
}
ヘルパーの変更例
require_once APPLICATION_HELPERS."CssCrush.php";
class Configurable_Helper_CssCrush extends Zend_View_Helper_CssCrush {
protected $__config_enabled = true;
public function __disable(){
$this->__config_enabled = false;
return $this;
}
public function __enable(){
$this->__config_enabled = true;
return $this;
}
public function cssCrush(){
if ($this->__config_enabled){
return parent::cssCrush();
} else{
return new Modified_CssCrush();
}
}
}
class Modified_CssCrush {
public static function file ( $file, $options = null ) {
return $file;
}
}
APPLICATION_HELPERS は /public/index.php で "../application/views/helpers/" として定義されています。
ここで、構成可能なヘルパーを追加したい場合は、元のヘルパーを "/application/views/helpers/" に置き、それを修正したバージョンを "/library/ConfigHelpers" に上記の例の構造で作成します。