我正在尝试找到一种方法来禁用“application/views/helpers”内的一些 View 助手...
我真正想要的是在 application.ini 上添加一些选项来启用或禁用某些帮助程序。
application.ini 示例:
helpers.Helper1=on
helpers.Helper2=off
现在的问题是,当一个助手关闭时,我想重写这个助手的一些函数,以便在 View 上返回不同的结果。这样,我不需要更改 View 脚本中的任何内容。
我想为每个助手在不同的位置有 2 个不同的 php 文件。一个使用真正的助手,另一个使用更改后的助手(在 application.ini 上关闭时工作)。
问题是我不知道如何告诉 View 应该加载哪一个......
有谁知道怎么做吗?
最终代码
好的,经过多次尝试,我将其与以下代码一起使用:
引导
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”上创建其修改版本,其结构如下上面的例子。
最佳答案
我认为你想要的是 Dependency Injection即将推出 zf2 ,但在 zf1 中不可用。
通过一些修补,你可以获得你需要的东西。
在 Bootstrap 中配置助手
(假设默认项目结构)
查看帮助程序路径配置:application/configs/application.ini:
resources.view.helperPath.Zf_View_Helper_ = "Zf/View/Helper"
一个简单的可配置帮助程序,(允许禁用/启用,但您显然可以添加所需的任何方法(使用它作为需要该行为的帮助程序的基类)
class Zf_View_Helper_Configurable extends Zend_View_Helper_Abstract
{
protected $isEnabled = true;
public function configurable()
{
return $this;
}
public function disable()
{
$this->isEnabled = false;
return $this;
}
public function enable()
{
$this->isEnabled = true;
return $this;
}
public function __toString()
{
if ($this->isEnabled) {
return 'Configurable is enabled';
} else {
return 'Configurable is disabled';
}
}
}
并在 Bootstrap 中配置助手:
public function _initConfigureHelpers()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$configurableHelper = $view->configurable();
$configurableHelper->disable();
}
您可以在 .ini 文件中添加选项并在 Bootstrap initConfigureHelpers() 方法中获取它们。
如果您希望任何默认的 zf 帮助程序具有此行为,请执行 @Ratzo 所说的操作并扩展这些帮助程序并添加所需的行为,然后在 Bootstrap 中配置它们。
关于zend-framework - 如何禁用某些 Zend View Helpers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10418829/